<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title> 朝花夕拾</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/ww.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      
<link rel="stylesheet" href="/css/fonts/remixicon.css">

      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.staticfile.org/pace/1.2.4/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    </head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      
<section class="cover">
    
  <div class="cover-frame">
    <div class="bg-box">
      <img src="/images/cover8.jpg" alt="image frame" />
    </div>
    <div class="cover-inner text-center text-white">
      <h1><a href="/">朝花夕拾</a></h1>
      <div id="subtitle-box">
        
        <span id="subtitle"></span>
        
      </div>
      <div>
        
      </div>
    </div>
  </div>
  <div class="cover-learn-more">
    <a href="javascript:void(0)" class="anchor"><i class="ri-arrow-down-line"></i></a>
  </div>
</section>



<script src="https://cdn.staticfile.org/typed.js/2.0.12/typed.min.js"></script>


<!-- Subtitle -->

  <script>
    try {
      var typed = new Typed("#subtitle", {
        strings: ['', '', ''],
        startDelay: 0,
        typeSpeed: 200,
        loop: true,
        backSpeed: 100,
        showCursor: true
      });
    } catch (err) {
      console.log(err)
    }
  </script>
  
<div id="main">
  <section class="outer">
  
  
  

<div class="notice" style="margin-top:50px">
    <i class="ri-heart-fill"></i>
    <div class="notice-content" id="broad"></div>
</div>
<script type="text/javascript">
    fetch('https://v1.hitokoto.cn')
        .then(response => response.json())
        .then(data => {
            document.getElementById("broad").innerHTML = data.hitokoto;
        })
        .catch(console.error)
</script>

<style>
    .notice {
        padding: 20px;
        border: 1px dashed #e6e6e6;
        color: #969696;
        position: relative;
        display: inline-block;
        width: 100%;
        background: #fbfbfb50;
        border-radius: 10px;
    }

    .notice i {
        float: left;
        color: #999;
        font-size: 16px;
        padding-right: 10px;
        vertical-align: middle;
        margin-top: -2px;
    }

    .notice-content {
        display: initial;
        vertical-align: middle;
    }
</style>
  
  <article class="articles">
    
    
    
    
    <article
  id="post-自制工具：dumpPEpage-sh"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/06/15/%E8%87%AA%E5%88%B6%E5%B7%A5%E5%85%B7%EF%BC%9AdumpPEpage-sh/"
    >自制工具：dumpPEpage.sh</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/06/15/%E8%87%AA%E5%88%B6%E5%B7%A5%E5%85%B7%EF%BC%9AdumpPEpage-sh/" class="article-date">
  <time datetime="2023-06-15T12:28:57.000Z" itemprop="datePublished">2023-06-15</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/GBase8s/">GBase8s</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="下载PE里面的每个页-bitmap和datahome"><a href="#下载PE里面的每个页-bitmap和datahome" class="headerlink" title="下载PE里面的每个页 bitmap和datahome"></a>下载PE里面的每个页 bitmap和datahome</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">filePe=&quot;db01:t6.pe&quot;</span><br><span class="line">dbname=&quot;db01&quot;</span><br><span class="line">tabename=&quot;t6&quot;</span><br><span class="line">rootPagesize=2</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Chunk=0</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Pathname=0</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Pagesize=0</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Offset=0</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">Size=0</span></span><br><span class="line">cat $filePe |grep -E &quot;xxxxx|$&#123;dbname&#125;:$&#123;tabname&#125;&quot; |awk &#x27;&#123;</span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">5==<span class="string">&quot;xxxxx&quot;</span>)&#123;</span></span><br><span class="line">		Chunk=$1;</span><br><span class="line">		Pathname=$2;</span><br><span class="line">		Pagesize=$3;</span><br><span class="line">	&#125;else&#123;</span><br><span class="line">	    dbtab=$1;</span><br><span class="line">		Offset=$2;</span><br><span class="line">		Size=$3;</span><br><span class="line">		print dbtab,Chunk,Pathname,Pagesize,Offset,Size ;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;&#x27; &gt; $&#123;dbname&#125;:$&#123;tabename&#125;.pe.line</span><br><span class="line"></span><br><span class="line">rm -fr datapageDIR</span><br><span class="line">mkdir datapageDIR</span><br><span class="line">cat $&#123;dbname&#125;:$&#123;tabename&#125;.pe.line |while read dbtab Chunk Pathname Pagesize Offset Size</span><br><span class="line">do</span><br><span class="line">	i=$Offset</span><br><span class="line">	let e=$Offset+$Size</span><br><span class="line">	while [ $i -lt $e ];do</span><br><span class="line">		outfile=datapageDIR/$&#123;dbtab&#125;_$&#123;i&#125;; &gt;$outfile;</span><br><span class="line">		let skip=$&#123;i&#125;*$&#123;Pagesize&#125;*1</span><br><span class="line">		let seek=0</span><br><span class="line">		let bs=1024</span><br><span class="line">		let count=$&#123;Pagesize&#125;*1</span><br><span class="line">		dd1 $Pathname $outfile $skip $seek $bs $count 1 1</span><br><span class="line">		let i=$i+1</span><br><span class="line">	done</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/GBase8s/" rel="tag">GBase8s</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/level3/" rel="tag">level3</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E8%87%AA%E7%A0%94/" rel="tag">自研</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-自制工具：dumpTBLSpaceAndPE-sh"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/06/15/%E8%87%AA%E5%88%B6%E5%B7%A5%E5%85%B7%EF%BC%9AdumpTBLSpaceAndPE-sh/"
    >自制工具：dumpTBLSpaceAndPE.sh</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/06/15/%E8%87%AA%E5%88%B6%E5%B7%A5%E5%85%B7%EF%BC%9AdumpTBLSpaceAndPE-sh/" class="article-date">
  <time datetime="2023-06-15T12:28:40.000Z" itemprop="datePublished">2023-06-15</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/GBase8s/">GBase8s</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="宕机环境下，导出某个表的pe列表"><a href="#宕机环境下，导出某个表的pe列表" class="headerlink" title="宕机环境下，导出某个表的pe列表"></a>宕机环境下，导出某个表的pe列表</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br></pre></td><td class="code"><pre><span class="line">rm -fr wangweipe</span><br><span class="line">mkdir -p wangweipe</span><br><span class="line">cd wangweipe</span><br><span class="line">export INFORMIXCONTIME=1</span><br><span class="line">export INFORMIXCONRETRY=0</span><br><span class="line">export TMPRINTOFFSETS=1</span><br><span class="line">export GBASEDBTCONTIME=1</span><br><span class="line">export GBASEDBTCONRETRY=0</span><br><span class="line">oncheck -pr &gt; pr</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">处理DBS</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   DBspace number                 10</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   DBspace name                   dbs02</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   Flags                          0x60001    No mirror chunks</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   Number of chunks               4</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   First chunk                    10</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   Pagesize (k)                   2</span></span><br><span class="line">awk &#x27;BEGIN &#123;printf(&quot;\n%-20s%-20s%-20s%-20s%-20s%-20s\n&quot;,&quot;DBspace number&quot;,&quot;DBspace name&quot;,&quot;Flags&quot;,&quot;Number of chunks&quot;,&quot;First chunk&quot;,&quot;Pagesize&quot;)&#125;&#x27; &gt; pr.dbs</span><br><span class="line">cat pr |sed -n &#x27;/Using DBspace page/,/Using primary chunk page/p&#x27; |grep -E &quot;DBspace number|DBspace name|Flags|Number of chunks|First chunk|Pagesize&quot; |awk &#x27;&#123;</span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/DBspace number/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s&quot;</span>,<span class="variable">$3</span>)&#125;</span></span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/DBspace name/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s&quot;</span>,<span class="variable">$3</span>)&#125;</span></span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/Flags/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s&quot;</span>,<span class="variable">$2</span>)&#125;</span></span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/Number of chunks/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s&quot;</span>,<span class="variable">$4</span>)&#125;</span></span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/First chunk/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s&quot;</span>,<span class="variable">$3</span>)&#125;</span></span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/Pagesize/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s\n&quot;</span>,<span class="variable">$3</span>)&#125;</span></span><br><span class="line">&#125;&#x27; &gt;&gt; pr.dbs</span><br><span class="line">cat pr.dbs</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">处理CHUNK</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   Chunk number                   12</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   Flags                          0x10040    Chunk is online</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   Chunk path                     /data/g31/datachk02_3</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   Chunk offset                   0 (p)</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   Chunk size                     5000 (p)</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   Number of free pages           0</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   DBspace number                 10</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">   Next chunk <span class="keyword">in</span> DBspace          13</span></span><br><span class="line">awk &#x27;BEGIN &#123;printf(&quot;\n%-20s%-20s%-50s%-20s%-20s%-20s%-20s\n&quot;,&quot;Chunk number&quot;,&quot;Flags&quot;,&quot;Chunk path&quot;,&quot;Chunk offset&quot;,&quot;Chunk size&quot;,&quot;Number of free page&quot;,&quot;DBspace number&quot;,&quot;Next chunk in DBspace&quot;)&#125;&#x27; &gt; pr.chunk</span><br><span class="line">cat pr |sed -n &#x27;/Using primary chunk page/,/Using archive page/p&#x27; |grep -E &quot;Chunk number|Flags|Chunk path|Chunk offset|Chunk size|Number of free pages|DBspace number|Next chunk in DBspace&quot; |awk &#x27;&#123;</span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/Chunk number/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s&quot;</span>,<span class="variable">$3</span>)&#125;</span></span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/Flags/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s&quot;</span>,<span class="variable">$2</span>)&#125;</span></span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/Chunk path/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-50s&quot;</span>,<span class="variable">$3</span>)&#125;</span></span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/Chunk offset/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s&quot;</span>,<span class="variable">$3</span>)&#125;</span></span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/Chunk size/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s&quot;</span>,<span class="variable">$3</span>)&#125;</span></span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/Number of free pages/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s&quot;</span>,<span class="variable">$5</span>)&#125;</span></span><br><span class="line"><span class="meta prompt_">	if($</span><span class="language-bash">0~/DBspace number/)&#123;<span class="built_in">printf</span>(<span class="string">&quot;%-20s\n&quot;</span>,<span class="variable">$3</span>)&#125;</span></span><br><span class="line">&#125;&#x27; &gt;&gt; pr.chunk</span><br><span class="line">cat pr.chunk</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">处理DBS的槽位</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">dumpDBS的TBL的extent_t的槽位：</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">DBSNAME=datadbs01</span></span><br><span class="line">echo &quot;请输入dbs名称：&quot;</span><br><span class="line">read DBSNAME</span><br><span class="line">OUT=dd.dbs.p4</span><br><span class="line">OUT2=dd.dbs.p4.slot5</span><br><span class="line">OUT3=dd.dbs.extents</span><br><span class="line">DBSPageS=$(cat pr.dbs|awk -v n=$DBSNAME &#x27;&#123;if($2==n) &#123;print $NF&#125;&#125;&#x27;)</span><br><span class="line">RTSPageS=$(cat pr.dbs|awk -v n=&quot;rootdbs&quot; &#x27;&#123;if($2==n) &#123;print $NF&#125;&#125;&#x27;)</span><br><span class="line">DBSID=$(cat pr.dbs|awk -v n=$DBSNAME &#x27;&#123;if($2==n) &#123;print $5&#125;&#125;&#x27;)</span><br><span class="line">DBSPS=$(cat pr.dbs|awk -v n=$DBSNAME &#x27;&#123;if($2==n) &#123;print $NF*1024&#125;&#125;&#x27;)</span><br><span class="line">DBSPATH=$(cat pr.chunk| awk -v id=$DBSID &#x27;$1==id &#123;print $3&#125;&#x27;)</span><br><span class="line">DBSPATHOFFSET=$(cat pr.chunk| awk -v id=$DBSID &#x27;$1==id &#123;print $4*1024&#125;&#x27;)</span><br><span class="line">let SKIPByte=$DBSPS*4+$DBSPATHOFFSET</span><br><span class="line">if [ $DBSNAME == &quot;rootdbs&quot; ]; then</span><br><span class="line">	let SKIPByte=$DBSPS*14+$DBSPATHOFFSET</span><br><span class="line">fi</span><br><span class="line">dd if=$DBSPATH of=$OUT skip=$SKIPByte bs=1 count=$DBSPS</span><br><span class="line">BEGIND=$(xxd $OUT |tail -2 |head -1|awk &#x27;&#123;print $6&#125;&#x27;|cut -c 1,2)</span><br><span class="line">BEGING=$(xxd $OUT |tail -2 |head -1|awk &#x27;&#123;print $6&#125;&#x27;|cut -c 3,4)</span><br><span class="line">BEGINs=&quot;0x$&#123;BEGING&#125;$&#123;BEGIND&#125;&quot;</span><br><span class="line">LEND=$(xxd $OUT |tail -2 |head -1|awk &#x27;&#123;print $7&#125;&#x27;|cut -c 1,2)</span><br><span class="line">LENG=$(xxd $OUT |tail -2 |head -1|awk &#x27;&#123;print $7&#125;&#x27;|cut -c 3,4)</span><br><span class="line">LENs=&quot;0x$&#123;LENG&#125;$&#123;LEND&#125;&quot;</span><br><span class="line">BEGIN=$(printf &quot;%d&quot; $BEGINs)</span><br><span class="line">LEN=$(printf &quot;%d&quot; $LENs)</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">dumpDBS的TBL的slot5内容：</span></span><br><span class="line">dd if=$OUT of=$OUT2 skip=$BEGIN bs=1 count=$LEN</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">10个一分,打印extent： 4len 2chunk 4begin</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash"><span class="variable">$OUT3</span></span></span><br><span class="line">i=0</span><br><span class="line">let count=($LEN-10)/10</span><br><span class="line">while [ $i -lt $count ]</span><br><span class="line">do</span><br><span class="line">	let sk=$i*10+4</span><br><span class="line">	dd if=$OUT2 of=$&#123;OUT2&#125;.$&#123;i&#125; skip=$sk bs=1 count=10</span><br><span class="line">	pchunk=$(xxd $&#123;OUT2&#125;.$&#123;i&#125; |awk &#x27;&#123;printf(&quot;%s%s&quot;,&quot;0x&quot;,$2)&#125;&#x27;)</span><br><span class="line">	pbegin=$(xxd $&#123;OUT2&#125;.$&#123;i&#125; |awk &#x27;&#123;printf(&quot;%s%s%s&quot;,&quot;0x&quot;,$3,$4)&#125;&#x27;)</span><br><span class="line">	plen=$(xxd $&#123;OUT2&#125;.$&#123;i&#125; |awk &#x27;&#123;printf(&quot;%s%s%s&quot;,&quot;0x&quot;,$5,$6)&#125;&#x27;)</span><br><span class="line">	pchunkd=$(printf &quot;%d&quot; $pchunk)</span><br><span class="line">	pbegind=$(printf &quot;%d&quot; $pbegin)</span><br><span class="line">	plend=$(printf &quot;%d&quot; $plen)</span><br><span class="line">	DBSPATHC=$(cat pr.chunk| awk -v id=$pchunkd &#x27;$1==id &#123;print $3&#125;&#x27;)</span><br><span class="line">	echo $pchunk $pbegin $plen $pchunkd $pbegind $plend $DBSPageS $DBSPATHC &gt;&gt; $OUT3</span><br><span class="line">	let i=$i+1</span><br><span class="line">done</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">cat</span> <span class="variable">$OUT3</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">加工一下OUT3，2k页/10进制bgein/10进制len</span> </span><br><span class="line">echo $DBSPageS  $RTSPageS</span><br><span class="line">cat $OUT3 |awk -v oc=0 -v DPS=$DBSPageS  -v RPS=$RTSPageS &#x27;&#123;</span><br><span class="line">	printf(&quot;%12s %12s %12s %12d %12d %12d %10d %s\n&quot;,$1,$2,$3,$4,$5/(DPS/RPS),$6-oc,$7,$8)</span><br><span class="line">	oc=$6</span><br><span class="line">&#125;&#x27; &gt; tmpfile</span><br><span class="line">mv tmpfile $OUT3</span><br><span class="line">cat $OUT3</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">我搞懂了slot5的内容了：</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">1、前面4个字节不要</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">2、10个字节一份（2chunk，4beging，4len汇总）</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">3、后面6个字节不要</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">处理以oncheck-pe格式输出</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">DBSNAME=datadbs01</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">OUT3=dd.dbs.extents</span></span><br><span class="line">chunknew=&quot;new&quot;</span><br><span class="line">privchunk=0</span><br><span class="line">cat $OUT3 | while read xc xb xl chunk begin len pagesize path</span><br><span class="line">do</span><br><span class="line"></span><br><span class="line">if [ $&#123;chunk&#125; -eq  $&#123;privchunk&#125; ] ; then</span><br><span class="line">	chunknew=&quot;old&quot;</span><br><span class="line">else </span><br><span class="line">	chunknew=&quot;new&quot;</span><br><span class="line">	privchunk=$chunk</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [ $chunknew == &quot;new&quot; ] ; then</span><br><span class="line">echo &quot;&quot;</span><br><span class="line">echo &quot;&quot;</span><br><span class="line">echo &quot;DBspace Usage Report: $DBSNAME           Owner: wangwei  Created: 05/09/2022&quot;</span><br><span class="line">echo &quot;&quot;</span><br><span class="line">echo &quot;&quot;</span><br><span class="line">echo &quot; Chunk Pathname                             Pagesize(k)  Size(p)  Used(p)  Free(p)&quot;</span><br><span class="line">echo &quot;     $&#123;chunk&#125; $&#123;path&#125;                           $&#123;pagesize&#125;   xxxxx     xxxxx   xxxxx&quot;</span><br><span class="line">echo &quot;&quot;</span><br><span class="line">echo &quot; Description                                                   Offset(p)  Size(p)&quot;</span><br><span class="line">echo &quot; ------------------------------------------------------------- -------- --------&quot;</span><br><span class="line">echo &quot; RESERVED PAGES                                                       0        2&quot;</span><br><span class="line">echo &quot; CHUNK FREELIST PAGE                                                  2        1&quot;</span><br><span class="line">fi</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">echo</span> <span class="string">&quot; datadbs01:TBLSpace                                            <span class="variable">$begin</span>       <span class="variable">$len</span>&quot;</span></span></span><br><span class="line">awk -v b=$begin -v l=$len -v pt=&quot;$&#123;DBSNAME&#125;:TBLSpace&quot; &#x27;BEGIN &#123;printf(&quot; %-61s %8d %8d\n&quot;,pt,b,l)&#125;&#x27;</span><br><span class="line"></span><br><span class="line">done &gt; $&#123;DBSNAME&#125;&quot;:TBLSpace&quot;.pe</span><br><span class="line">cat $&#123;DBSNAME&#125;&quot;:TBLSpace&quot;.pe</span><br><span class="line"></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">处理 dump所有partation页</span></span><br><span class="line">OUT3=dd.dbs.extents</span><br><span class="line">ddonepage()&#123;</span><br><span class="line"><span class="meta prompt_">	#</span><span class="language-bash">RuCan: path begin pagesize outdir</span></span><br><span class="line">	outdir=$(printf &quot;%s%03x&quot; &quot;0x&quot; $chunk)</span><br><span class="line">	outfil=$(printf &quot;%s%08x&quot; &quot;0x&quot; $begin)</span><br><span class="line">	let pages=$&#123;pagesize&#125;*1024</span><br><span class="line">	echo &quot;dd if=$path of=$&#123;outdir&#125;/$&#123;outfil&#125; skip=$begin bs=$pages count=1&quot;</span><br><span class="line">	dd if=$path of=$&#123;outdir&#125;/$&#123;outfil&#125; skip=$begin bs=$pages count=1 2&gt;/dev/null</span><br><span class="line">&#125;</span><br><span class="line">cat $OUT3 | while read xc xb xl chunk begin len pagesize path</span><br><span class="line">do</span><br><span class="line">	outdir=$(printf &quot;%s%03x&quot; &quot;0x&quot; $chunk)</span><br><span class="line">	rm -fr $outdir</span><br><span class="line">	mkdir -p $outdir</span><br><span class="line">done</span><br><span class="line">cat $OUT3 | while read xc xb xl chunk begin len pagesize path</span><br><span class="line">do</span><br><span class="line">	i=0</span><br><span class="line">	while [ $i -lt $len ]; do</span><br><span class="line">		ddonepage</span><br><span class="line">		let i=$i+1</span><br><span class="line">		let begin=$begin+1</span><br><span class="line">	done</span><br><span class="line">done</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">xxd 0x008/0x00000003 |grep <span class="string">&quot;b0: &quot;</span>|grep -v <span class="string">&quot;0000 0000 0000 0000 0000 0000 0000 0000&quot;</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">xxd 0x008/0x00000109 |grep <span class="string">&quot;b0: &quot;</span>|grep -v <span class="string">&quot;0000 0000 0000 0000 0000 0000 0000 0000&quot;</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">xxd 0x008/0x000002d7 |grep <span class="string">&quot;a0: &quot;</span>|grep -v <span class="string">&quot;0000 0000 0000 0000 0000 0000 0000 0000&quot;</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">xxd 0x008/0x00000587 |grep <span class="string">&quot;b0: &quot;</span>|grep -v <span class="string">&quot;0000 0000 0000 0000 0000 0000 0000 0000&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">dump目标表partation页 db01:t1</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">这个程序没必要写成自动的，不划算！(其实也可以写成自动，把00000a0:和00000b0:的字段一合并就行了)</span></span><br><span class="line">cd ../  ##此时在dd</span><br><span class="line">partationdir=partationDIR</span><br><span class="line">rm -fr $&#123;partationdir&#125;</span><br><span class="line">mkdir -p $&#123;partationdir&#125;</span><br><span class="line">echo &quot;请输入你要抢救数据的表所在的数据库名称：&quot;</span><br><span class="line">read dbname</span><br><span class="line">echo &quot;请输入你要抢救数据的表名称：&quot;</span><br><span class="line">read tbname</span><br><span class="line">ls -ld wangweipe/0x* |awk &#x27;&#123;print $NF&#125;&#x27; |while read dir</span><br><span class="line">do</span><br><span class="line">	cd $dir</span><br><span class="line">	ls -l 0x* |awk &#x27;&#123;print $NF&#125;&#x27;  |while read file</span><br><span class="line">    do</span><br><span class="line">    	xxd -c 256 $file|head -1|grep &quot;$&#123;dbname&#125;\..*\.$&#123;tbname&#125;\.&quot;</span><br><span class="line">    	if [ $? == 0 ] ;then</span><br><span class="line">    		cp $&#123;file&#125; ../../$&#123;partationdir&#125;/$&#123;file&#125;</span><br><span class="line">    		echo &quot;这个文件就是你要找的partation页！！！  $&#123;partationdir&#125;/$&#123;file&#125;&quot;</span><br><span class="line">			echo &quot;$&#123;dbname&#125;:$&#123;tbname&#125; $&#123;file&#125;&quot; &gt;&gt; ../../$&#123;partationdir&#125;/list.index</span><br><span class="line">    	fi</span><br><span class="line">	done</span><br><span class="line">	cd ../../</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">dumpDBS的TBL的extent_t的槽位： 0保留 1保留 2ChunkFree 3TBLSpaceBitmap （所以要跳过4页）</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">dumpTable时（begin后面加1页(bitmap)即可,算了不跳，保留bitmap）</span></span><br><span class="line">cd wangweipe</span><br><span class="line">OUT4=dd.tab.p.extents.ALL</span><br><span class="line"><span class="meta prompt_">&gt;</span><span class="language-bash"><span class="variable">$OUT4</span></span></span><br><span class="line">ls -l ../$&#123;partationdir&#125;/0x* |awk &#x27;&#123;print $NF&#125;&#x27; |while read filepath</span><br><span class="line">do</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">DBSNAME=datadbs01</span></span><br><span class="line">OUT=$filepath</span><br><span class="line">OUT2=dd.tab.p.slot5</span><br><span class="line">OUT3=dd.tab.p.extents</span><br><span class="line"><span class="meta prompt_">&gt;</span><span class="language-bash"><span class="variable">$OUT2</span></span></span><br><span class="line"><span class="meta prompt_">&gt;</span><span class="language-bash"><span class="variable">$OUT3</span></span></span><br><span class="line">DBSPageS=$(cat pr.dbs|awk -v n=$DBSNAME &#x27;&#123;if($2==n) &#123;print $NF&#125;&#125;&#x27;)</span><br><span class="line">RTSPageS=$(cat pr.dbs|awk -v n=&quot;rootdbs&quot; &#x27;&#123;if($2==n) &#123;print $NF&#125;&#125;&#x27;)</span><br><span class="line">DBSID=$(cat pr.dbs|awk -v n=$DBSNAME &#x27;&#123;if($2==n) &#123;print $5&#125;&#125;&#x27;)</span><br><span class="line">DBSPS=$(cat pr.dbs|awk -v n=$DBSNAME &#x27;&#123;if($2==n) &#123;print $NF*1024&#125;&#125;&#x27;)</span><br><span class="line">DBSPATH=$(cat pr.chunk| awk -v id=$DBSID &#x27;$1==id &#123;print $3&#125;&#x27;)</span><br><span class="line">DBSPATHOFFSET=$(cat pr.chunk| awk -v id=$DBSID &#x27;$1==id &#123;print $4*1024&#125;&#x27;)</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">let</span> SKIPByte=<span class="variable">$DBSPS</span>*4+<span class="variable">$DBSPATHOFFSET</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="keyword">if</span> [ <span class="variable">$DBSNAME</span> == <span class="string">&quot;rootdbs&quot;</span> ]; <span class="keyword">then</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">	<span class="built_in">let</span> SKIPByte=<span class="variable">$DBSPS</span>*14+<span class="variable">$DBSPATHOFFSET</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="keyword">fi</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">dd</span> <span class="keyword">if</span>=<span class="variable">$DBSPATH</span> of=<span class="variable">$OUT</span> skip=<span class="variable">$SKIPByte</span> bs=1 count=<span class="variable">$DBSPS</span></span></span><br><span class="line">BEGIND=$(xxd $OUT |tail -2 |head -1|awk &#x27;&#123;print $6&#125;&#x27;|cut -c 1,2)</span><br><span class="line">BEGING=$(xxd $OUT |tail -2 |head -1|awk &#x27;&#123;print $6&#125;&#x27;|cut -c 3,4)</span><br><span class="line">BEGINs=&quot;0x$&#123;BEGING&#125;$&#123;BEGIND&#125;&quot;</span><br><span class="line">LEND=$(xxd $OUT |tail -2 |head -1|awk &#x27;&#123;print $7&#125;&#x27;|cut -c 1,2)</span><br><span class="line">LENG=$(xxd $OUT |tail -2 |head -1|awk &#x27;&#123;print $7&#125;&#x27;|cut -c 3,4)</span><br><span class="line">LENs=&quot;0x$&#123;LENG&#125;$&#123;LEND&#125;&quot;</span><br><span class="line">BEGIN=$(printf &quot;%d&quot; $BEGINs)</span><br><span class="line">LEN=$(printf &quot;%d&quot; $LENs)</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">dumpDBS的TBL的slot5内容：</span></span><br><span class="line">dd if=$OUT of=$OUT2 skip=$BEGIN bs=1 count=$LEN</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">10个一分,打印extent： 4len 2chunk 4begin</span></span><br><span class="line"><span class="meta prompt_">&gt; </span><span class="language-bash"><span class="variable">$OUT3</span></span></span><br><span class="line">i=0</span><br><span class="line">let count=($LEN-10)/10</span><br><span class="line">while [ $i -lt $count ]</span><br><span class="line">do</span><br><span class="line">	let sk=$i*10+4</span><br><span class="line">	dd if=$OUT2 of=$&#123;OUT2&#125;.$&#123;i&#125; skip=$sk bs=1 count=10</span><br><span class="line">	pchunk=$(xxd $&#123;OUT2&#125;.$&#123;i&#125; |awk &#x27;&#123;printf(&quot;%s%s&quot;,&quot;0x&quot;,$2)&#125;&#x27;)</span><br><span class="line">	pbegin=$(xxd $&#123;OUT2&#125;.$&#123;i&#125; |awk &#x27;&#123;printf(&quot;%s%s%s&quot;,&quot;0x&quot;,$3,$4)&#125;&#x27;)</span><br><span class="line">	plen=$(xxd $&#123;OUT2&#125;.$&#123;i&#125; |awk &#x27;&#123;printf(&quot;%s%s%s&quot;,&quot;0x&quot;,$5,$6)&#125;&#x27;)</span><br><span class="line">	pchunkd=$(printf &quot;%d&quot; $pchunk)</span><br><span class="line">	pbegind=$(printf &quot;%d&quot; $pbegin)</span><br><span class="line">	plend=$(printf &quot;%d&quot; $plen)</span><br><span class="line">	DBSPATHC=$(cat pr.chunk| awk -v id=$pchunkd &#x27;$1==id &#123;print $3&#125;&#x27;)</span><br><span class="line">	echo $pchunk $pbegin $plen $pchunkd $pbegind $plend $DBSPageS $DBSPATHC &gt;&gt; $OUT3</span><br><span class="line">	let i=$i+1</span><br><span class="line">done</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">cat</span> <span class="variable">$OUT3</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">加工一下OUT3，2k页/10进制bgein/10进制len</span> </span><br><span class="line">echo $DBSPageS  $RTSPageS</span><br><span class="line">cat $OUT3 |awk -v oc=0 -v DPS=$DBSPageS  -v RPS=$RTSPageS &#x27;&#123;</span><br><span class="line">	printf(&quot;%12s %12s %12s %12d %12d %12d %10d %s\n&quot;,$1,$2,$3,$4,$5/(DPS/RPS),$6-oc,$7,$8)</span><br><span class="line">	oc=$6</span><br><span class="line">&#125;&#x27; &gt; tmpfile</span><br><span class="line">mv tmpfile $OUT3</span><br><span class="line">cat $OUT3 &gt;&gt; $OUT4 </span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">我搞懂了slot5的内容了：</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">1、前面4个字节不要</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">2、10个字节一分（2chunk，4beging，4len汇总）</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">3、后面6个字节不要</span></span><br><span class="line">done</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">处理以oncheck-pe格式输出</span></span><br><span class="line">DBSNAME=$dbname</span><br><span class="line">TABNAME=$tbname</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">OUT3=dd.tab.p.extents</span></span><br><span class="line">chunknew=&quot;new&quot;</span><br><span class="line">privchunk=0</span><br><span class="line">cat $OUT4 | while read xc xb xl chunk begin len pagesize path</span><br><span class="line">do</span><br><span class="line"></span><br><span class="line">if [ $&#123;chunk&#125; -eq  $&#123;privchunk&#125; ] ; then</span><br><span class="line">	chunknew=&quot;old&quot;</span><br><span class="line">else </span><br><span class="line">	chunknew=&quot;new&quot;</span><br><span class="line">	privchunk=$chunk</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [ $chunknew == &quot;new&quot; ] ; then</span><br><span class="line">echo &quot;&quot;</span><br><span class="line">echo &quot;&quot;</span><br><span class="line">echo &quot;DBspace Usage Report: $DBSNAME           Owner: wangwei  Created: 05/09/2022&quot;</span><br><span class="line">echo &quot;&quot;</span><br><span class="line">echo &quot;&quot;</span><br><span class="line">echo &quot; Chunk Pathname                             Pagesize(k)  Size(p)  Used(p)  Free(p)&quot;</span><br><span class="line">echo &quot;     $&#123;chunk&#125; $&#123;path&#125;                           $&#123;pagesize&#125;   xxxxx     xxxxx   xxxxx&quot;</span><br><span class="line">echo &quot;&quot;</span><br><span class="line">echo &quot; Description                                                   Offset(p)  Size(p)&quot;</span><br><span class="line">echo &quot; ------------------------------------------------------------- -------- --------&quot;</span><br><span class="line">echo &quot; RESERVED PAGES                                                       0        2&quot;</span><br><span class="line">echo &quot; CHUNK FREELIST PAGE                                                  2        1&quot;</span><br><span class="line">fi</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">echo</span> <span class="string">&quot; datadbs01:TBLSpace                                            <span class="variable">$begin</span>       <span class="variable">$len</span>&quot;</span></span></span><br><span class="line">awk -v b=$begin -v l=$len -v pt=&quot;$&#123;DBSNAME&#125;:$&#123;TABNAME&#125;&quot; &#x27;BEGIN &#123;printf(&quot; %-61s %8d %8d\n&quot;,pt,b,l)&#125;&#x27;</span><br><span class="line"></span><br><span class="line">done &gt; &quot;$&#123;DBSNAME&#125;:$&#123;TABNAME&#125;.pe&quot;</span><br><span class="line">echo &quot;&quot; &gt;&gt; &quot;$&#123;DBSNAME&#125;:$&#123;TABNAME&#125;.pe&quot;</span><br><span class="line">cat &quot;$&#123;DBSNAME&#125;:$&#123;TABNAME&#125;.pe&quot;</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">cp</span> <span class="string">&quot;<span class="variable">$&#123;DBSNAME&#125;</span>:<span class="variable">$&#123;TABNAME&#125;</span>.pe&quot;</span> /root/c/dumpTable/.pe.log</span></span><br><span class="line"></span><br><span class="line">cp &quot;$&#123;DBSNAME&#125;:$&#123;TABNAME&#125;.pe&quot; ../</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/GBase8s/" rel="tag">GBase8s</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/level3/" rel="tag">level3</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E8%87%AA%E7%A0%94/" rel="tag">自研</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-最新版本KeepalivedGBase8s-leopard-猎豹版"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/06/15/%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%ACKeepalivedGBase8s-leopard-%E7%8C%8E%E8%B1%B9%E7%89%88/"
    >最新版本KeepalivedGBase8s(leopard)猎豹版</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/06/15/%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%ACKeepalivedGBase8s-leopard-%E7%8C%8E%E8%B1%B9%E7%89%88/" class="article-date">
  <time datetime="2023-06-15T12:16:37.000Z" itemprop="datePublished">2023-06-15</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/GBase8s/">GBase8s</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="部署方法"><a href="#部署方法" class="headerlink" title="部署方法"></a>部署方法</h1><p>0， 提取软件<br>      链接：<a target="_blank" rel="noopener" href="https://pan.baidu.com/s/1U6DBLTeKjffnaawOyt6vTw">https://pan.baidu.com/s/1U6DBLTeKjffnaawOyt6vTw</a><br>      提取码：6789</p>
<p>1,  编辑 config_files.txt 填写你的真实数据</p>
<p>2,  执行  .&#x2F;config_files.sh</p>
<p>3,  把 config_files 目录的文件上传到正确的为位置即可。</p>
<p>4，按照正常步骤搭建数据HDR集群，复制关系正常后，关闭两个数据库实例。</p>
<p>5，去掉keepalive的随机自启动：systemctl disable keepalived</p>
<p>6、开启gbasedbtd的随机自启动：systemctl enable gbasedbtd</p>
<p>7，启动主数据库节点：systemctl restart gbasedbtd （会自动带启keepalived）</p>
<p>8，启动从数据库节点：systemctl restart gbasedbtd（会自动带启keepalived）</p>
<h1 id="软件说明"><a href="#软件说明" class="headerlink" title="软件说明"></a>软件说明</h1><ul>
<li><p>功能</p>
<ol>
<li><p>提供vip</p>
</li>
<li><p>提供故障停止库和切换</p>
</li>
<li><p>提供启动时检查是否有主，有加入集群，没有则自启动为主。</p>
</li>
</ol>
</li>
<li><p>组件</p>
<ol>
<li>每2秒执行&#x2F;opt&#x2F;gbkeepalived&#x2F;gbase8s_check.sh检查GBase数据库是否可用，如果不可用，停止keeplive进程。</li>
<li>keeplive升级为主时执行&#x2F;opt&#x2F;gbkeepalived&#x2F;master.sh停止复制关系并升级为主数据库。</li>
<li>keeplive停止时执行&#x2F;opt&#x2F;gbkeepalived&#x2F;stop.sh停止数据库。</li>
<li>oninit.sh为启动脚本，启动数据库时检查集群是否有主存活，有加入集群，没有则自启动为主。并启动keeplive进程。</li>
</ol>
</li>
</ul>
<h1 id="部署步骤"><a href="#部署步骤" class="headerlink" title="部署步骤"></a>部署步骤</h1><h3 id="一、部署环境介绍"><a href="#一、部署环境介绍" class="headerlink" title="一、部署环境介绍"></a>一、部署环境介绍</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">		IP地址        主机名  实例名</span><br><span class="line">节点1：  192.168.1.11  h11   g330pri</span><br><span class="line">节点2：  192.168.1.12  h12   g330hdr</span><br><span class="line">VIP:    192.168.1.13        g330vip</span><br><span class="line"></span><br><span class="line">节点1和节点2 之间搭建HDR集群，DRAUTO设置为0.</span><br><span class="line">节点1和节点2 为一个keepalived组。</span><br></pre></td></tr></table></figure>



<h3 id="二、下载安装keepalived服务"><a href="#二、下载安装keepalived服务" class="headerlink" title="二、下载安装keepalived服务"></a>二、下载安装keepalived服务</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">[root@h11 opt]# tar zxvf keepalived-2.2.7.tar.gz</span><br><span class="line">[root@h12 opt]# tar zxvf keepalived-2.2.7.tar.gz</span><br><span class="line"></span><br><span class="line">[root@h11 keepalived-2.2.7]# yum -y install gcc</span><br><span class="line">[root@h11 keepalived-2.2.7]# yum -y install openssl</span><br><span class="line">[root@h11 keepalived-2.2.7]# yum -y install openssl-devel</span><br><span class="line">[root@h11 opt]# cd keepalived-2.2.7</span><br><span class="line">[root@h11 keepalived-2.2.7]# ./configure --prefix=/usr/local/keepalived --sysconf=/etc</span><br><span class="line">[root@h11 keepalived-2.2.7]# make &amp;&amp; make install</span><br><span class="line">[root@h11 keepalived-2.2.7]# whereis keepalived</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">把keepalive注册为系统服务，但不要开启随机自启动</span></span><br><span class="line">cd /opt/keepalived-2.2.7/keepalived/etc</span><br><span class="line">cp init.d/keepalived /etc/init.d/</span><br><span class="line">cp sysconfig/keepalived /etc/sysconfig/</span><br><span class="line">systemctl daemon-reload</span><br><span class="line">systemctl start keepalived</span><br><span class="line">systemctl disable keepalived</span><br><span class="line">systemctl stop keepalived</span><br></pre></td></tr></table></figure>



<h3 id="三、安装数据库软件"><a href="#三、安装数据库软件" class="headerlink" title="三、安装数据库软件"></a>三、安装数据库软件</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">略.....</span></span><br></pre></td></tr></table></figure>



<h3 id="四、生产配置文件"><a href="#四、生产配置文件" class="headerlink" title="四、生产配置文件"></a>四、生产配置文件</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">上传配置文件的模板 ---------------------------------------------------</span></span><br><span class="line">pc@pc:~/keepdir$ ll</span><br><span class="line">-rw-rw-r--  1 pc pc  4256 6月  13 12:54 config_files.sh</span><br><span class="line">-rw-rw-r--  1 pc pc   285 6月  13 12:54 config_files.txt</span><br><span class="line">-rw-rw-r--  1 pc pc 24206 6月  13 12:54 std.config_files.zip</span><br><span class="line">pc@pc:~/keepdir$</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看并修改你的真实参数值 ---------------------------------------------------</span></span><br><span class="line">pc@pc:~/keepdir$ cat config_files.txt</span><br><span class="line">server_nodes 2</span><br><span class="line"></span><br><span class="line">server_pri_node g330pri</span><br><span class="line">server_sec_node g330hdr</span><br><span class="line">server_vip_name g330vip</span><br><span class="line"></span><br><span class="line">server_pri_priority 100</span><br><span class="line">server_sec_priority 90</span><br><span class="line"></span><br><span class="line">server_pri_ip 192.168.1.11/24 9083</span><br><span class="line">server_sec_ip 192.168.1.12/24 9083</span><br><span class="line">server_vip_ip 192.168.1.13/24 9088</span><br><span class="line"></span><br><span class="line">server_pri_eth eth0</span><br><span class="line">server_sec_eth eth0</span><br><span class="line"></span><br><span class="line">pc@pc:~/keepdir$</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">解压配置文件模板包  ---------------------------------------------------</span></span><br><span class="line">pc@pc:~/keepdir$ unzip std.config_files.zip</span><br><span class="line">Archive:  std.config_files.zip</span><br><span class="line">   creating: std.config_files/</span><br><span class="line">  inflating: std.config_files/bash_profile.std</span><br><span class="line">  inflating: std.config_files/config.txt.std</span><br><span class="line">  inflating: std.config_files/crt_gbasedbtd.sh.std</span><br><span class="line">  inflating: std.config_files/gbase8s_check.sh.std</span><br><span class="line">  inflating: std.config_files/keepalived.conf.std</span><br><span class="line">  inflating: std.config_files/master.sh.std</span><br><span class="line">  inflating: std.config_files/onconfig.std</span><br><span class="line">  inflating: std.config_files/oninit.sh.std</span><br><span class="line">  inflating: std.config_files/ontape.sh.std</span><br><span class="line">  inflating: std.config_files/sqlhosts.std</span><br><span class="line">  inflating: std.config_files/stop.sh.std</span><br><span class="line">pc@pc:~/keepdir$ </span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">执行config_files.sh脚本，生成配置文件 ---------------------------------</span></span><br><span class="line">pc@pc:~/keepdir$ pwd</span><br><span class="line">/home/pc/keepdir</span><br><span class="line">pc@pc:~/keepdir$ ll</span><br><span class="line">-rw-rw-r--  1 pc pc  4256 6月  13 11:24 config_files.sh</span><br><span class="line">-rw-rw-r--  1 pc pc   285 6月  13 11:13 config_files.txt</span><br><span class="line">drwxrwxr-x  2 pc pc  4096 6月  13 11:10 std.config_files/</span><br><span class="line">-rw-rw-r--  1 pc pc 23862 6月  13 11:20 std.config_files.zip</span><br><span class="line">pc@pc:~/keepdir$ sh config_files.sh</span><br><span class="line">pc@pc:~/keepdir$</span><br><span class="line">pc@pc:~/keepdir$ ll</span><br><span class="line">drwxrwxr-x  2 pc pc  4096 6月  13 11:27 config_files/      #执行完成会生成这个目录</span><br><span class="line">-rw-rw-r--  1 pc pc  4256 6月  13 11:24 config_files.sh</span><br><span class="line">-rw-rw-r--  1 pc pc   285 6月  13 11:13 config_files.txt</span><br><span class="line">drwxrwxr-x  2 pc pc  4096 6月  13 11:10 std.config_files/</span><br><span class="line">-rw-rw-r--  1 pc pc 23862 6月  13 11:20 std.config_files.zip</span><br><span class="line">pc@pc:~/keepdir$</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">复制配置文件到指定位置  ---------------------------------------------------</span></span><br><span class="line">cd /home/pc/keepdir/config_files</span><br><span class="line"></span><br><span class="line">scp bash_profile_g330pri root@192.168.1.11:/home/gbasedbt/.bash_profile</span><br><span class="line">scp bash_profile_g330hdr root@192.168.1.12:/home/gbasedbt/.bash_profile</span><br><span class="line"></span><br><span class="line">scp keepalived.conf_g330pri_100 root@192.168.1.11:/etc/keepalived/keepalived.conf</span><br><span class="line">scp keepalived.conf_g330hdr_90  root@192.168.1.12:/etc/keepalived/keepalived.conf</span><br><span class="line"></span><br><span class="line">scp gbase8s_check.sh root@192.168.1.11:/home/gbasedbt/</span><br><span class="line">scp gbase8s_check.sh root@192.168.1.12:/home/gbasedbt/</span><br><span class="line"></span><br><span class="line">scp master.sh root@192.168.1.11:/home/gbasedbt/</span><br><span class="line">scp master.sh root@192.168.1.12:/home/gbasedbt/</span><br><span class="line"></span><br><span class="line">scp stop.sh root@192.168.1.11:/home/gbasedbt/</span><br><span class="line">scp stop.sh root@192.168.1.12:/home/gbasedbt/</span><br><span class="line"></span><br><span class="line">GBASEDBTDIR=/opt/install/g330</span><br><span class="line">scp oninit.sh root@192.168.1.11:$GBASEDBTDIR/bin/</span><br><span class="line">scp oninit.sh root@192.168.1.12:$GBASEDBTDIR/bin/</span><br><span class="line"></span><br><span class="line">scp sqlhosts root@192.168.1.11:$GBASEDBTDIR/etc/</span><br><span class="line">scp sqlhosts root@192.168.1.12:$GBASEDBTDIR/etc/</span><br><span class="line"></span><br><span class="line">scp onconfig.g330pri root@192.168.1.11:$GBASEDBTDIR/etc/</span><br><span class="line">scp onconfig.g330hdr root@192.168.1.12:$GBASEDBTDIR/etc/</span><br><span class="line"></span><br><span class="line">scp crt_gbasedbtd.sh root@192.168.1.11:/home/gbasedbt/</span><br><span class="line">scp crt_gbasedbtd.sh root@192.168.1.12:/home/gbasedbt/</span><br></pre></td></tr></table></figure>



<h3 id="五、搭建数据库集群"><a href="#五、搭建数据库集群" class="headerlink" title="五、搭建数据库集群"></a>五、搭建数据库集群</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在192.168.1.11和192.168.1.12上执行 --------------------------------</span></span><br><span class="line">su - gbasedbt</span><br><span class="line">sed -i &#x27;s/192.168.1.13/0.0.0.0/&#x27; $GBASEDBTDIR/etc/sqlhosts</span><br><span class="line">sed -i &#x27;s/^FULL_DISK_INIT.*$/FULL_DISK_INIT 1/&#x27; $GBASEDBTDIR/etc/$ONCONFIG</span><br><span class="line">sed -i &#x27;s/^PHYSFILE.*$/#PHYSFILE/&#x27; $GBASEDBTDIR/etc/$ONCONFIG</span><br><span class="line">sed -i &#x27;s/^LOGFILES.*$/#LOGFILES/&#x27; $GBASEDBTDIR/etc/$ONCONFIG</span><br><span class="line">sed -i &#x27;s/^LOGSIZE.*$/#LOGSIZE/&#x27; $GBASEDBTDIR/etc/$ONCONFIG</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在192.168.1.11上执行 -----------------------------------------------</span></span><br><span class="line">oninit -ivy</span><br><span class="line"></span><br><span class="line">ontape -s -L 0 -t STDIO -F |ssh 192.168.1.12 &quot;. /home/gbasedbt/.bash_profile; onmode -ky;onclean -ky;ontape -p -t STDIO&quot;</span><br><span class="line"></span><br><span class="line">onmode -d primary g330hdr</span><br><span class="line"></span><br><span class="line">ssh 192.168.1.12 &quot;. /home/gbasedbt/.bash_profile; onmode -d secondary g330pri&quot;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">GBase123!</span></span><br></pre></td></tr></table></figure>



<h3 id="六、关闭数据库"><a href="#六、关闭数据库" class="headerlink" title="六、关闭数据库"></a>六、关闭数据库</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在192.168.1.12上执行 --------------------------------</span></span><br><span class="line">onmode -ky</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在192.168.1.11上执行 --------------------------------</span></span><br><span class="line">onmode -ky</span><br></pre></td></tr></table></figure>

<h3 id="七、配置gbasedbtd服务"><a href="#七、配置gbasedbtd服务" class="headerlink" title="七、配置gbasedbtd服务"></a>七、配置gbasedbtd服务</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">root用户在192.168.1.11和192.168.1.12上执行 --------------------------------</span></span><br><span class="line">/home/gbasedbt/crt_gbasedbtd.sh</span><br></pre></td></tr></table></figure>

<h3 id="八、启动gbasedbtd服务"><a href="#八、启动gbasedbtd服务" class="headerlink" title="八、启动gbasedbtd服务"></a>八、启动gbasedbtd服务</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在192.168.1.11上执行 --------------------------------</span></span><br><span class="line">systemctl start gbasedbtd</span><br><span class="line">systemctl status gbasedbtd</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">等等主实例正常后</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">再在192.168.1.12上执行 --------------------------------</span></span><br><span class="line">systemctl start gbasedbtd</span><br><span class="line">systemctl status gbasedbtd</span><br></pre></td></tr></table></figure>

<h3 id="九、检查keepalived是否启动"><a href="#九、检查keepalived是否启动" class="headerlink" title="九、检查keepalived是否启动"></a>九、检查keepalived是否启动</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在192.168.1.11和192.168.1.12上执行 --------------------------------</span></span><br><span class="line">ps -ef |grep keep</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="十、检查数据库集群和VIP"><a href="#十、检查数据库集群和VIP" class="headerlink" title="十、检查数据库集群和VIP"></a>十、检查数据库集群和VIP</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在192.168.1.11和192.168.1.12上执行 --------------------------------</span></span><br><span class="line">ip a |grep 192.168.1.13</span><br><span class="line">onstat -g dri</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="十一、查看KeepalivedGBase8s运行日志"><a href="#十一、查看KeepalivedGBase8s运行日志" class="headerlink" title="十一、查看KeepalivedGBase8s运行日志"></a>十一、查看KeepalivedGBase8s运行日志</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">在192.168.1.11和192.168.1.12上执行 --------------------------------</span></span><br><span class="line">tail -20 /tmp/online.keeplive.log</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">修复内容记录：2023年6月12日</span><br><span class="line">	1、完善了readmi文档，简化了部署流程。</span><br><span class="line">	2、修复了gbase8s_check.sh里面对killall环境的检测判断。</span><br><span class="line">	3、修复了ononit.sh里面的启动不成功情况，清理自身。</span><br><span class="line">	4、修复了crt_gbasedbtd.sh配置和启动分离。</span><br><span class="line">	5、修复了keepalived自启动冲突，有gbasedbtd服务统一管理。</span><br><span class="line">	6、统一了日志输出到 /tmp/online.keeplive.log。</span><br><span class="line"></span><br><span class="line">修复内容记录：2023年6月13日 （打包标记：leopard）</span><br><span class="line">	7、修复了sqlhosts0.0.0.0 配置。 </span><br><span class="line">	8、修复了LANG环境变量也设置了en.US。</span><br><span class="line">	9、修复了gbase8s_check.sh关闭数据库和keepalived的日志输入出。</span><br><span class="line">	10、完善了oninit.sh启动时尝试10次找集群中主节点的尝试。</span><br><span class="line">	11、完善了oninit.sh检测结果的日志输出。</span><br><span class="line">	12、完善了master.sh升级为主节点后自动重启一遍vip上的监听服务。</span><br><span class="line">	13、完善了sopt.sh的日志输出。</span><br><span class="line"></span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/GBasedbtd/" rel="tag">GBasedbtd</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Keepalived/" rel="tag">Keepalived</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E8%87%AA%E7%A0%94/" rel="tag">自研</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E9%9B%86%E7%BE%A4/" rel="tag">集群</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E9%AB%98%E5%8F%AF%E7%94%A8/" rel="tag">高可用</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-ESQLC测试GBase8s游标稳定性读"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/06/15/ESQLC%E6%B5%8B%E8%AF%95GBase8s%E6%B8%B8%E6%A0%87%E7%A8%B3%E5%AE%9A%E6%80%A7%E8%AF%BB/"
    >ESQLC测试GBase8s游标稳定性读</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/06/15/ESQLC%E6%B5%8B%E8%AF%95GBase8s%E6%B8%B8%E6%A0%87%E7%A8%B3%E5%AE%9A%E6%80%A7%E8%AF%BB/" class="article-date">
  <time datetime="2023-06-15T11:31:23.000Z" itemprop="datePublished">2023-06-15</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/GBase8s/">GBase8s</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="GBase8s游标稳定性读"><a href="#GBase8s游标稳定性读" class="headerlink" title="GBase8s游标稳定性读"></a>GBase8s游标稳定性读</h1><h2 id="基础库表建立"><a href="#基础库表建立" class="headerlink" title="基础库表建立"></a>基础库表建立</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> database db03 <span class="keyword">with</span> log;</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> customer(</span><br><span class="line">	fname <span class="type">char</span>(<span class="number">9</span>),</span><br><span class="line">    lname <span class="type">char</span>(<span class="number">9</span>)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> customer <span class="keyword">values</span>(<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;A&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> customer <span class="keyword">values</span>(<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;B&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> customer <span class="keyword">values</span>(<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;C&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> customer <span class="keyword">values</span>(<span class="string">&#x27;aa&#x27;</span>,<span class="string">&#x27;AA&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> customer <span class="keyword">values</span>(<span class="string">&#x27;bb&#x27;</span>,<span class="string">&#x27;BB&#x27;</span>);</span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> customer lock mode (<span class="type">row</span>);</span><br></pre></td></tr></table></figure>

<h2 id="游标稳定性读取行"><a href="#游标稳定性读取行" class="headerlink" title="游标稳定性读取行"></a>游标稳定性读取行</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line">EXEC SQL define FNAME_LEN <span class="number">15</span>;</span><br><span class="line">EXEC SQL define LNAME_LEN <span class="number">15</span>;</span><br><span class="line">main()</span><br><span class="line">&#123;</span><br><span class="line">  EXEC SQL BEGIN DECLARE SECTION;</span><br><span class="line">  <span class="type">char</span> fname[ FNAME_LEN + <span class="number">1</span> ];</span><br><span class="line">  <span class="type">char</span> lname[ LNAME_LEN + <span class="number">1</span> ];</span><br><span class="line">  EXEC SQL END DECLARE SECTION;</span><br><span class="line"></span><br><span class="line">  <span class="built_in">printf</span>( <span class="string">&quot;DEMO1 Sample ESQL Program running.\n\n&quot;</span>);</span><br><span class="line">  EXEC SQL WHENEVER ERROR STOP;</span><br><span class="line">  EXEC SQL connect to <span class="string">&#x27;db03&#x27;</span>;</span><br><span class="line"></span><br><span class="line">  EXEC SQL DECLARE democursor cursor <span class="keyword">for</span></span><br><span class="line">  select fname, lname</span><br><span class="line">  into :fname, :lname</span><br><span class="line">  from customer</span><br><span class="line">  where lname &lt; <span class="string">&#x27;C&#x27;</span>  ;</span><br><span class="line">  EXEC SQL execute immediate <span class="string">&#x27;BEGIN WORK&#x27;</span>;</span><br><span class="line">  EXEC SQL execute immediate <span class="string">&#x27;SET   ISOLATION   TO   CURSOR  STABILITY&#x27;</span>;</span><br><span class="line"></span><br><span class="line">  EXEC SQL open democursor;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (;;)</span><br><span class="line">  &#123;</span><br><span class="line">    getchar();</span><br><span class="line">    EXEC SQL fetch democursor;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">strncmp</span>(SQLSTATE, <span class="string">&quot;00&quot;</span>, <span class="number">2</span>) != <span class="number">0</span>)</span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%s %s\n&quot;</span>,fname, lname);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (<span class="built_in">strncmp</span>(SQLSTATE, <span class="string">&quot;02&quot;</span>, <span class="number">2</span>) != <span class="number">0</span>)</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;SQLSTATE after fetch is %s\n&quot;</span>, SQLSTATE);</span><br><span class="line"></span><br><span class="line">  EXEC SQL execute immediate <span class="string">&#x27;COMMIT WORK&#x27;</span>;</span><br><span class="line">  EXEC SQL close democursor;</span><br><span class="line">  EXEC SQL <span class="built_in">free</span> democursor;</span><br><span class="line">  EXEC SQL disconnect current;</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">&quot;\nDEMO1 Sample Program over.\n\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line">        </span><br></pre></td></tr></table></figure>

<p>说明： 执行cs.exe程序，每按一次“回车”，程序的游标就会指向一行数据，此时这行数据就会被加上共享锁，别人就不能修改这行数据了。当游标指向下一行数据时，前面的一行数据就可以修改了。</p>
<h2 id="测试步骤："><a href="#测试步骤：" class="headerlink" title="测试步骤："></a>测试步骤：</h2><table>
<thead>
<tr>
<th>结果</th>
<th>会话1：</th>
<th>会话2：</th>
<th>窗口3：</th>
</tr>
</thead>
<tbody><tr>
<td></td>
<td>.&#x2F;cs.exe</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>“回车”</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>onstat -g sql 可以看到CURSOR  STABILITY隔离级别。 onstat -k 可以看到只有第1行锁定了，其他行没有锁定。</td>
</tr>
<tr>
<td>锁报错update执行失败</td>
<td></td>
<td>update customer set lname&#x3D;’A1’ where fname&#x3D;’a’ ;</td>
<td></td>
</tr>
<tr>
<td>update执行成功</td>
<td></td>
<td>update customer set lname&#x3D;’B1’ where fname&#x3D;’b’ ;</td>
<td></td>
</tr>
<tr>
<td></td>
<td>“回车”</td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>onstat -g sql 可以看到CURSOR  STABILITY隔离级别。 onstat -k 可以看到只有第2行锁定了，其他行没有锁定。</td>
</tr>
<tr>
<td>update执行成功</td>
<td></td>
<td>update customer set lname&#x3D;’A2’ where fname&#x3D;’a’ ;</td>
<td></td>
</tr>
<tr>
<td>锁报错update执行失败</td>
<td></td>
<td>update customer set lname&#x3D;’B2’ where fname&#x3D;’b’ ;</td>
<td></td>
</tr>
</tbody></table>
<h1 id="使用ESQLC连接GBase8s数据库插入一条数据"><a href="#使用ESQLC连接GBase8s数据库插入一条数据" class="headerlink" title="使用ESQLC连接GBase8s数据库插入一条数据"></a>使用ESQLC连接GBase8s数据库插入一条数据</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line">EXEC SQL define FNAME_LEN <span class="number">15</span>;</span><br><span class="line">EXEC SQL define LNAME_LEN <span class="number">15</span>;</span><br><span class="line">main()</span><br><span class="line">&#123;</span><br><span class="line">  EXEC SQL BEGIN DECLARE SECTION;</span><br><span class="line">  <span class="type">char</span> fname[ FNAME_LEN + <span class="number">1</span> ];</span><br><span class="line">  <span class="type">char</span> lname[ LNAME_LEN + <span class="number">1</span> ];</span><br><span class="line">  EXEC SQL END DECLARE SECTION;</span><br><span class="line"></span><br><span class="line">  <span class="built_in">printf</span>( <span class="string">&quot;DEMO1 Sample ESQL Program running.\n\n&quot;</span>);</span><br><span class="line">  EXEC SQL WHENEVER ERROR STOP;</span><br><span class="line">  EXEC SQL connect to <span class="string">&#x27;db03@hdr2&#x27;</span>;</span><br><span class="line"></span><br><span class="line">  EXEC SQL execute immediate <span class="string">&quot;insert into customer values(&#x27;a11&#x27;,&#x27;b111&#x27;)&quot;</span>;</span><br><span class="line">  EXEC SQL execute immediate <span class="string">&#x27;BEGIN WORK&#x27;</span>;</span><br><span class="line">  EXEC SQL execute immediate <span class="string">&#x27;SET   ISOLATION   TO   CURSOR  STABILITY&#x27;</span>;</span><br><span class="line"></span><br><span class="line">  EXEC SQL execute immediate <span class="string">&#x27;COMMIT WORK&#x27;</span>;</span><br><span class="line">  EXEC SQL disconnect current;</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">&quot;\nDEMO1 Sample Program over.\n\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/ESQLC/" rel="tag">ESQLC</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/GBase8s/" rel="tag">GBase8s</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-C语言：笔记202305"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/05/23/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E7%AC%94%E8%AE%B0202305/"
    >C语言：笔记202305</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/05/23/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E7%AC%94%E8%AE%B0202305/" class="article-date">
  <time datetime="2023-05-23T06:17:08.000Z" itemprop="datePublished">2023-05-23</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/C%E8%AF%AD%E8%A8%80/">C语言</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="C语言：笔记202305"><a href="#C语言：笔记202305" class="headerlink" title="C语言：笔记202305"></a>C语言：笔记202305</h1><h2 id="关键字extern"><a href="#关键字extern" class="headerlink" title="关键字extern"></a>关键字extern</h2><p>​	extern关键字：再找找别急着报错。</p>
<h2 id="变量作用域-scope"><a href="#变量作用域-scope" class="headerlink" title="变量作用域(scope)"></a>变量作用域(scope)</h2><ol>
<li><p>代码块作用域  ：{}</p>
</li>
<li><p>文件作用域      ：写的地方到文件结束</p>
</li>
<li><p>原型作用域      ：函数原型声明的时候的变量名可以随便 写</p>
</li>
<li><p>函数作用域      ：配合goto语句的标签</p>
</li>
</ol>
<h2 id="链接属性"><a href="#链接属性" class="headerlink" title="链接属性"></a>链接属性</h2><p>​	编译器编译c源码和基础库做链接，生成可执行程序。</p>
<ul>
<li>external   可以去别的文件里面去找具有文件作用域的对象</li>
<li>internal    用static关键字来限制external对象，限制后只能本文件内访问</li>
<li>none         代码块变量是空链接属性</li>
</ul>
<h2 id="生存期"><a href="#生存期" class="headerlink" title="生存期"></a>生存期</h2><ul>
<li>静态存储期 （static storage duration）：文件作用域，程序退出释放。</li>
<li>自动存储期 （automatic storage duration）： 代码块作用域，代码块结束时效</li>
</ul>
<h2 id="变量存储类型"><a href="#变量存储类型" class="headerlink" title="变量存储类型"></a>变量存储类型</h2><ol>
<li>auto         ：自动存储区，代码块中的变量默认是atuo类型</li>
<li>register   ： 自动存储区，寄存器变量是有可能存储在cpu内部的</li>
<li>static       ： 静态存储区，静态局部变量的生存期和全局变量一样但是作用域还是块</li>
<li>extern     ： 声明外部再找找</li>
<li>typedef   ： 对类型的封装，给类型取别名，配合结构体使用更方便</li>
</ol>
<h2 id="关键字struct"><a href="#关键字struct" class="headerlink" title="关键字struct"></a>关键字struct</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Date</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">int</span> year;</span><br><span class="line">    <span class="type">int</span> mouth;</span><br><span class="line">    <span class="type">int</span> day;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">Date</span> *<span class="title">date</span>;</span></span><br><span class="line">    date = (<span class="keyword">struct</span> Date *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">struct</span> Date));</span><br><span class="line">    <span class="keyword">if</span> (date == <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;内存分配失败\n&quot;</span>);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    date-&gt;year = <span class="number">2023</span>;</span><br><span class="line">    date-&gt;mouth = <span class="number">5</span>;</span><br><span class="line">    date-&gt;day = <span class="number">20</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d-%d-%d&quot;</span>, date-&gt;year, date-&gt;mouth, date-&gt;day);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="关键字typedef"><a href="#关键字typedef" class="headerlink" title="关键字typedef"></a>关键字typedef</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">Date</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">int</span> year;</span><br><span class="line">    <span class="type">int</span> mouth;</span><br><span class="line">    <span class="type">int</span> day;</span><br><span class="line">&#125; DATE, *P_DATE;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    P_DATE date;</span><br><span class="line">    date = (P_DATE)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(DATE));</span><br><span class="line">    <span class="keyword">if</span> (date == <span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;内存分配失败\n&quot;</span>);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    date-&gt;year = <span class="number">2023</span>;</span><br><span class="line">    date-&gt;mouth = <span class="number">5</span>;</span><br><span class="line">    date-&gt;day = <span class="number">20</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%d-%d-%d&quot;</span>, date-&gt;year, date-&gt;mouth, date-&gt;day);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="指针函数和函数指针"><a href="#指针函数和函数指针" class="headerlink" title="指针函数和函数指针"></a>指针函数和函数指针</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    函数指针            int (*fp)(int, int)</span></span><br><span class="line"><span class="comment">    函数指针作入参       </span></span><br><span class="line"><span class="comment">    函数指针作返回值  </span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">add</span><span class="params">(<span class="type">int</span>, <span class="type">int</span>)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">sub</span><span class="params">(<span class="type">int</span>, <span class="type">int</span>)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">compt</span><span class="params">(<span class="type">int</span> (*)(<span class="type">int</span>,<span class="type">int</span>),<span class="type">int</span>,<span class="type">int</span>)</span>;</span><br><span class="line"><span class="type">int</span> (*select(<span class="type">char</span>))(<span class="type">int</span>, <span class="type">int</span>);</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">add</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a+b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">sub</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a-b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数指针作入参的例子</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">compt</span><span class="params">(<span class="type">int</span> (*fp)(<span class="type">int</span>,<span class="type">int</span>),<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (*fp)(a,b);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数指针作返回值的例子</span></span><br><span class="line"><span class="type">int</span> (*select(<span class="type">char</span> o))(<span class="type">int</span> a,<span class="type">int</span> b)&#123;</span><br><span class="line">    <span class="keyword">switch</span> (o)</span><br><span class="line">    &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;+&#x27;</span>:</span><br><span class="line">        <span class="keyword">return</span> add;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;-&#x27;</span>:</span><br><span class="line">        <span class="keyword">return</span> sub;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> rest;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 函数指针作入参的例子</span></span><br><span class="line">    rest = compt(add,<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a+b = %d\n&quot;</span>,rest);</span><br><span class="line">    rest = compt(sub,<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a-b = %d\n&quot;</span>,rest);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 函数指针作返回值的例子</span></span><br><span class="line">    <span class="type">char</span> o;</span><br><span class="line">    <span class="type">int</span> a,b;</span><br><span class="line">    <span class="type">int</span> (*fp)(<span class="type">int</span>, <span class="type">int</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;请输入计算式（例如2+3）：&quot;</span>);</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%c%d&quot;</span>,&amp;a,&amp;o,&amp;b);</span><br><span class="line">    fp = select(o);</span><br><span class="line">    rest = compt(fp,a,b);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;rest = %d\n&quot;</span>,rest);   </span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="递归"><a href="#递归" class="headerlink" title="递归"></a>递归</h2><p>​	函数调用它本身。递归必须要有结束条件，否则程序会崩溃。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdio.h&gt;</span></span><br><span class="line"></span><br><span class="line">void func01(int);</span><br><span class="line"></span><br><span class="line">void func01(int n)</span><br><span class="line">&#123;</span><br><span class="line">    printf(&quot;%d aaaa\n&quot;,n);</span><br><span class="line">    if (--n)</span><br><span class="line">    &#123;</span><br><span class="line">        func01(n);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line">    func01(3);</span><br><span class="line">    func01(3);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdio.h&gt;</span></span><br><span class="line"></span><br><span class="line">long func01(int);</span><br><span class="line"></span><br><span class="line">long func01(int n)</span><br><span class="line">&#123;</span><br><span class="line">    long m;</span><br><span class="line">    if (n &gt; 0)</span><br><span class="line">    &#123;</span><br><span class="line">        m = n * func01(n-1);</span><br><span class="line">    &#125;</span><br><span class="line">    else</span><br><span class="line">    &#123;</span><br><span class="line">        m = 1;</span><br><span class="line">    &#125;</span><br><span class="line">    return m;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line">    long int a = func01(5);</span><br><span class="line">    printf(&quot;%d\n&quot;, a);</span><br><span class="line">    long int b = func01(5);</span><br><span class="line">    printf(&quot;%d\n&quot;, b);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a>快速排序</h2><p>​	快速排序算法的基本思想是：通过一趟排序将待排序数据分割成独立的两个部分，其中一部分的所有元素均比另一部分的元素小，然后分别对这两部分继续进行排序，重复上述步骤直到排序完成。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">print_list</span><span class="params">(<span class="type">int</span> *int01, <span class="type">int</span> length)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; length; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, *int01++);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">quick_sort</span><span class="params">(<span class="type">int</span> <span class="built_in">array</span>[], <span class="type">int</span> left, <span class="type">int</span> right)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i = left, j = right;</span><br><span class="line">    <span class="type">int</span> temp;</span><br><span class="line">    <span class="type">int</span> pivot;</span><br><span class="line">    pivot = <span class="built_in">array</span>[(left + right) / <span class="number">2</span>];</span><br><span class="line">    <span class="keyword">while</span> (i &lt; j)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 从左到右找到大于等于基准点的元素</span></span><br><span class="line">        <span class="keyword">while</span> (<span class="built_in">array</span>[i] &lt; pivot)</span><br><span class="line">        &#123;</span><br><span class="line">            i++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 从右到左找到小于等于基准点的元素</span></span><br><span class="line">        <span class="keyword">while</span> (<span class="built_in">array</span>[j] &gt; pivot)</span><br><span class="line">        &#123;</span><br><span class="line">            j--;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;currnt: %d,%d\n&quot;</span>, i, j);</span><br><span class="line">        <span class="comment">// 如果i&lt;=j，则互换</span></span><br><span class="line">        <span class="keyword">if</span> (i &lt;= j)</span><br><span class="line">        &#123;</span><br><span class="line">            temp = <span class="built_in">array</span>[i];</span><br><span class="line">            <span class="built_in">array</span>[i] = <span class="built_in">array</span>[j];</span><br><span class="line">            <span class="built_in">array</span>[j] = temp;</span><br><span class="line">            i++;</span><br><span class="line">            j--;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    print_list(&amp;<span class="built_in">array</span>[<span class="number">0</span>], <span class="number">9</span>);</span><br><span class="line">    <span class="keyword">if</span> (left &lt; j)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n  sort right array[%d,%d]\n&quot;</span>, left, j);</span><br><span class="line">        quick_sort(<span class="built_in">array</span>, left, j);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (i &lt; right)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n  sort left array[%d,%d]\n&quot;</span>, i, right);</span><br><span class="line">        quick_sort(<span class="built_in">array</span>, i, right);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> <span class="built_in">array</span>[] = &#123;<span class="number">9</span>, <span class="number">1</span>, <span class="number">8</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">2</span>, <span class="number">6</span>, <span class="number">5</span>, <span class="number">4</span>&#125;;</span><br><span class="line">    <span class="type">int</span> i, length;</span><br><span class="line"></span><br><span class="line">    length = <span class="keyword">sizeof</span>(<span class="built_in">array</span>) / <span class="keyword">sizeof</span>(<span class="built_in">array</span>[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n  sort all array[%d,%d]\n&quot;</span>, <span class="number">0</span>, length<span class="number">-1</span>);</span><br><span class="line">    quick_sort(<span class="built_in">array</span>, <span class="number">0</span>, length - <span class="number">1</span>);</span><br><span class="line">    print_list(&amp;<span class="built_in">array</span>[<span class="number">0</span>], length);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="内存管理函数"><a href="#内存管理函数" class="headerlink" title="内存管理函数"></a>内存管理函数</h2><ol>
<li><p>malloc   申请动态内存空间  viod *malloc(size_t size);  在堆上，需要手动释放。</p>
</li>
<li><p>free        释放动态内存空间  void free(void *ptr);释放堆上的内存</p>
</li>
<li><p>calloc     申请并初始化一系列内存空间  void *calloc(size_t nmemb, size_t size);初始为0。</p>
</li>
<li><p>realloc   重新分配内存空间     viod *realloc(void *ptr, size_t size);重新分配并拷贝。</p>
<p>（注明： 普通的局部变量在栈上。）</p>
</li>
<li><p>memset     使用一个常量字节填充内存空间    memset(ptr, 0, N * sizeof(int)); </p>
</li>
<li><p>memcpy      拷贝内存空间                                 memcpy(prt2,prt1,N);  free(ptr1);</p>
</li>
<li><p>memmove   拷贝内存空间</p>
</li>
<li><p>memcmp      比较内存空间</p>
</li>
<li><p>memchr        在内存空间中搜索一个字符</p>
</li>
</ol>
<p>根据用户输入动态扩容内存的例子：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdio.h&gt;</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdlib.h&gt;</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;string.h&gt;</span></span><br><span class="line"></span><br><span class="line">int main(void)</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    int num, i;</span><br><span class="line">    int count = 0;</span><br><span class="line">    int *ptr = NULL;</span><br><span class="line">    do</span><br><span class="line">    &#123;</span><br><span class="line">        printf(&quot;input number: &quot;);</span><br><span class="line">        scanf(&quot;%d&quot;, &amp;num);</span><br><span class="line">        count++;</span><br><span class="line">        ptr = (int *)realloc(ptr, count * sizeof(int));</span><br><span class="line">        if (ptr == NULL)</span><br><span class="line">        &#123;</span><br><span class="line">            exit(1);</span><br><span class="line">        &#125;</span><br><span class="line">        ptr[count - 1] = num;</span><br><span class="line">    &#125; while (num != -1);</span><br><span class="line"></span><br><span class="line">    for (i = 0; i &lt; count; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        printf(&quot;%d &quot;, ptr[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    putchar(&#x27;\n&#x27;);</span><br><span class="line">    free(ptr);</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="内存布局"><a href="#内存布局" class="headerlink" title="内存布局"></a>内存布局</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdio.h&gt;</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">include &lt;stdlib.h&gt;</span></span><br><span class="line"></span><br><span class="line">int global_uninit_var;</span><br><span class="line">int global_init_var1 = 520 ;</span><br><span class="line">int global_init_var2 = 880 ;</span><br><span class="line"></span><br><span class="line">void func(void);</span><br><span class="line">void func(void)&#123;</span><br><span class="line">	;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main(void)&#123;</span><br><span class="line">	int local_var1;</span><br><span class="line">	int local_var2;</span><br><span class="line">	</span><br><span class="line">	static int static_uninit_var;</span><br><span class="line">	static int static_init_var = 456;</span><br><span class="line">	</span><br><span class="line">	char *str1 = &quot;1111&quot;;                   //字符串常量</span><br><span class="line">	char *str2 = &quot;2222&quot;;</span><br><span class="line">	char str3[5] = &#123;&#x27;a&#x27;,&#x27;b&#x27;,&#x27;1&#x27;,&#x27;2&#x27;,&#x27;\0&#x27;&#125;; //局部变量</span><br><span class="line">	char str4[5] = &#123;&#x27;a&#x27;,&#x27;b&#x27;,&#x27;1&#x27;,&#x27;2&#x27;,&#x27;\0&#x27;&#125;; </span><br><span class="line">	</span><br><span class="line">	int *malloc_var = (int *)malloc(sizeof(int));</span><br><span class="line">	</span><br><span class="line">    printf(&quot;addr of local_var1 -&gt;        %p\n&quot;,&amp;local_var1);</span><br><span class="line">	printf(&quot;addr of local_var2 -&gt;        %p\n&quot;,&amp;local_var2);</span><br><span class="line">    printf(&quot;addr of str3 -&gt;              %p\n&quot;,str3);</span><br><span class="line">    printf(&quot;addr of str4 -&gt;              %p\n&quot;,str4);</span><br><span class="line">	printf(&quot;addr of malloc_var -&gt;        %p\n&quot;,malloc_var);</span><br><span class="line">	printf(&quot;addr of global_uninit_var -&gt; %p\n&quot;,&amp;global_uninit_var);</span><br><span class="line">	printf(&quot;addr of static_uninit_var -&gt; %p\n&quot;,&amp;static_uninit_var);</span><br><span class="line">	printf(&quot;addr of static_init_var -&gt;   %p\n&quot;,&amp;static_init_var);</span><br><span class="line">	printf(&quot;addr of global_init_var2 -&gt;  %p\n&quot;,&amp;global_init_var2);</span><br><span class="line">	printf(&quot;addr of global_init_var1 -&gt;  %p\n&quot;,&amp;global_init_var1);</span><br><span class="line">	printf(&quot;addr of str2 -&gt;              %p\n&quot;,str2);</span><br><span class="line">	printf(&quot;addr of str1 -&gt;              %p\n&quot;,str1);</span><br><span class="line">	printf(&quot;addr of func -&gt;              %p\n&quot;,func);</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">[root@wei07 c]# gcc t.c -o t &amp;&amp; ./t</span><br><span class="line">addr of local_var1 -&gt;        0x7ffc0d64f00c</span><br><span class="line">addr of local_var2 -&gt;        0x7ffc0d64f008</span><br><span class="line">addr of str3 -&gt;              0x7ffc0d64f000</span><br><span class="line">addr of str4 -&gt;              0x7ffc0d64eff0</span><br><span class="line">addr of malloc_var2 -&gt;       0x1d32030</span><br><span class="line">addr of malloc_var1 -&gt;       0x1d32010</span><br><span class="line">addr of global_uninit_var -&gt; 0x601050</span><br><span class="line">addr of static_uninit_var -&gt; 0x60104c</span><br><span class="line">addr of static_init_var -&gt;   0x601044</span><br><span class="line">addr of global_init_var2 -&gt;  0x601040</span><br><span class="line">addr of global_init_var1 -&gt;  0x60103c</span><br><span class="line">addr of str2 -&gt;              0x4007a5</span><br><span class="line">addr of str1 -&gt;              0x4007a0</span><br><span class="line">addr of func -&gt;              0x40057d</span><br><span class="line">[root@wei07 c]#</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<table>
<thead>
<tr>
<th>变量名称</th>
<th>变量类型</th>
<th>内存地址</th>
<th>内存段</th>
</tr>
</thead>
<tbody><tr>
<td>local_var1</td>
<td>局部变量</td>
<td>高（大）</td>
<td>栈stack</td>
</tr>
<tr>
<td>local_var2</td>
<td>局部变量</td>
<td></td>
<td>栈stack</td>
</tr>
<tr>
<td>str3[]</td>
<td>局部变量</td>
<td></td>
<td>栈stack</td>
</tr>
<tr>
<td>str4[]</td>
<td>局部变量</td>
<td></td>
<td>栈stack</td>
</tr>
<tr>
<td>malloc_var2</td>
<td>动态申请的内存空间</td>
<td></td>
<td>堆heap</td>
</tr>
<tr>
<td>malloc_var1</td>
<td>动态申请的内存空间</td>
<td></td>
<td>堆heap</td>
</tr>
<tr>
<td>global_uninit_var</td>
<td>全局变量（未初始化）</td>
<td></td>
<td>BSS</td>
</tr>
<tr>
<td>static_uninit_var</td>
<td>静态变量（未初始化）</td>
<td></td>
<td>BSS</td>
</tr>
<tr>
<td>static_init_var</td>
<td>静态变量（初始化）</td>
<td></td>
<td>数据段</td>
</tr>
<tr>
<td>global_init_var2</td>
<td>全局变量（初始化）</td>
<td></td>
<td>数据段</td>
</tr>
<tr>
<td>global_init_var1</td>
<td>全局变量（初始化）</td>
<td></td>
<td>数据段</td>
</tr>
<tr>
<td>str2</td>
<td>字符串常量</td>
<td></td>
<td>代码段</td>
</tr>
<tr>
<td>str1</td>
<td>字符串常量</td>
<td></td>
<td>代码段</td>
</tr>
<tr>
<td>func</td>
<td>函数</td>
<td>低（小）</td>
<td>代码段</td>
</tr>
</tbody></table>
<p>注明1：BSS是英文Block Started by Symbol的简称，这个区段的数据在程序运行前将被自动初始化为数字0.</p>
<p>注明2：linux命令size，显示程序的text，data，bss段的大小。程序的本质就是这3个段。</p>
<p>注明3：堆和栈的区别 （ 堆由程序员手动申请，栈由系统自动分配；堆到主动释放时结束，栈到局部函数返回时结束；不同函数可以自由访问堆，不同函数不能相互访问栈；堆由低往高用，栈由高往低用，他们共享一片蓝天。）</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">D:\soft\src\Python3\C&gt;gcc t.c -o t &amp;&amp; t</span><br><span class="line">addr of local_var1 -&gt;        000000000061FDFC</span><br><span class="line">addr of local_var2 -&gt;        000000000061FDF8</span><br><span class="line">addr of str3 -&gt;              000000000061FDF3</span><br><span class="line">addr of str4 -&gt;              000000000061FDEE</span><br><span class="line">addr of malloc_var2 -&gt;       0000000000736BD0</span><br><span class="line">addr of malloc_var1 -&gt;       0000000000736BB0</span><br><span class="line">addr of global_uninit_var -&gt; 0000000000407970</span><br><span class="line">addr of static_uninit_var -&gt; 0000000000407030</span><br><span class="line">addr of static_init_var -&gt;   0000000000403018</span><br><span class="line">addr of global_init_var2 -&gt;  0000000000403014</span><br><span class="line">addr of global_init_var1 -&gt;  0000000000403010</span><br><span class="line">addr of str2 -&gt;              0000000000404005</span><br><span class="line">addr of str1 -&gt;              0000000000404000</span><br><span class="line">addr of func -&gt;              0000000000401550</span><br><span class="line"></span><br><span class="line">D:\soft\src\Python3\C&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h2 id="UNIX接口-低级IO-read和write"><a href="#UNIX接口-低级IO-read和write" class="headerlink" title="UNIX接口-低级IO-read和write"></a>UNIX接口-低级IO-read和write</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;stdio.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;unistd.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BUF 6</span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">copy01</span><span class="params">()</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">getchar01</span><span class="params">()</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">getchar02</span><span class="params">()</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    copy01();</span><br><span class="line"></span><br><span class="line">    <span class="type">char</span> c;</span><br><span class="line">    c = getchar01();</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c\n&quot;</span>, c);</span><br><span class="line">    c = getchar01(); <span class="comment">// 等待read</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c\n&quot;</span>, c);</span><br><span class="line"></span><br><span class="line">    c = getchar02();</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c\n&quot;</span>, c);</span><br><span class="line">    c = getchar02();</span><br><span class="line">    c = getchar02();</span><br><span class="line">    c = getchar02(); <span class="comment">// 要么从缓存里面取，要么等待read</span></span><br><span class="line">    c = getchar02();</span><br><span class="line">    c = getchar02();</span><br><span class="line">    c = getchar02();</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c\n&quot;</span>, c);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 注明： </span></span><br><span class="line">    <span class="comment">//      如果把getchar02放到01前面执行，02会一次性尽量读取BUFSIZE512个字符，</span></span><br><span class="line">    <span class="comment">//      会发现01要等待第二次的标准输入。</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 复制标准输入到标准输出 */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">copy01</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">char</span> exit0 = <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">    <span class="type">char</span> buf[BUF];</span><br><span class="line">    <span class="type">int</span> n;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;请输入内容，回车结束。输入0时，退出复制。\n&quot;</span>);</span><br><span class="line">    <span class="keyword">while</span> ((n = read(<span class="number">0</span>, buf, BUF)) &gt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span> (buf[<span class="number">0</span>] == exit0)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;退出。\n&quot;</span>);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        write(<span class="number">1</span>, buf, n);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 不带缓冲的getchar */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">getchar01</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">char</span> c;</span><br><span class="line">    <span class="keyword">return</span> (read(<span class="number">0</span>, &amp;c, <span class="number">1</span>) == <span class="number">1</span>) ? (<span class="type">unsigned</span> <span class="type">char</span>)c : EOF;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 带缓冲的getchar */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">getchar02</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">static</span> <span class="type">char</span> buf[BUFSIZ];</span><br><span class="line">    <span class="type">static</span> <span class="type">char</span> *bufp = buf;</span><br><span class="line">    <span class="type">static</span> <span class="type">int</span> n = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (n == <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        n = read(<span class="number">0</span>, buf, <span class="keyword">sizeof</span>(buf));</span><br><span class="line">        bufp = buf;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> (--n &gt;= <span class="number">0</span>) ? (<span class="type">unsigned</span> <span class="type">char</span>)*bufp++ : EOF;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="共用体union"><a href="#共用体union" class="headerlink" title="共用体union"></a>共用体union</h2><p>​		特殊的结构体，里面的所有成员共用同一个地址，同一时间只能用里面的一个成员。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">union</span> <span class="title">Test</span>&#123;</span></span><br><span class="line">    <span class="type">int</span> t;</span><br><span class="line">    <span class="type">double</span> pi;</span><br><span class="line">    <span class="type">char</span> str[<span class="number">6</span>];</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="时间tm结构"><a href="#时间tm结构" class="headerlink" title="时间tm结构"></a>时间tm结构</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;time.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span>&#123;</span><br><span class="line">    strcut tm *p;</span><br><span class="line">    <span class="type">time_t</span> t;</span><br><span class="line">    time(&amp;t);          <span class="comment">//秒数赋值</span></span><br><span class="line">    p = localtime(&amp;t); <span class="comment">//tm结构体赋值</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">struct tm &#123;</span><br><span class="line">   int tm_sec;         /* 秒，范围从 0 到 59        */</span><br><span class="line">   int tm_min;         /* 分，范围从 0 到 59        */</span><br><span class="line">   int tm_hour;        /* 小时，范围从 0 到 23        */</span><br><span class="line">   int tm_mday;        /* 一月中的第几天，范围从 1 到 31    */</span><br><span class="line">   int tm_mon;         /* 月，范围从 0 到 11        */</span><br><span class="line">   int tm_year;        /* 自 1900 年起的年数        */</span><br><span class="line">   int tm_wday;        /* 一周中的第几天，范围从 0 到 6    */</span><br><span class="line">   int tm_yday;        /* 一年中的第几天，范围从 0 到 365    */</span><br><span class="line">   int tm_isdst;       /* 夏令时                */</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="枚举类型enum"><a href="#枚举类型enum" class="headerlink" title="枚举类型enum"></a>枚举类型enum</h2><p>​		如果一个变量有且仅有几种值，那么就适合用枚举类型。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">Week</span> &#123;</span>sun,mon,tue,wed,thu,fri,sat&#125;;</span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">Week</span> <span class="title">today</span>;</span></span><br><span class="line">    strunt tm *p;</span><br><span class="line">    <span class="type">time_t</span> t;</span><br><span class="line">    time(&amp;t);</span><br><span class="line">    p=localtime(&amp;t);</span><br><span class="line">    today=p-&gt;tm_wday;</span><br><span class="line">    <span class="keyword">return</span> today;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>注明：枚举就是符号常量，默认0，1，2，3…;  可以定义枚举类型的时候修改默认值，修改后呈现两面派：0，1，12，13…</p>
<h2 id="位域-节约空间"><a href="#位域-节约空间" class="headerlink" title="位域(:节约空间)"></a>位域(:节约空间)</h2><p>​		位域是一个成员站用几个2进制位。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Test</span>&#123;</span></span><br><span class="line">	<span class="type">unsigned</span> <span class="type">int</span> a:<span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> b:<span class="number">1</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> c:<span class="number">2</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="位运算"><a href="#位运算" class="headerlink" title="位运算"></a>位运算</h2><ul>
<li><p>~   按位取反</p>
</li>
<li><p>&amp;   按位与</p>
</li>
<li><p>^   按位非</p>
</li>
<li><p>|   按位或</p>
<p>注明：配合掩码使用很好。</p>
</li>
</ul>
<h2 id="移位运算"><a href="#移位运算" class="headerlink" title="移位运算"></a>移位运算</h2><p>​	value &lt;&lt; 2 ; 左移2位 </p>
<p>​    value &gt;&gt; 2 ; 右移2位</p>
<h2 id="打开和关闭文件"><a href="#打开和关闭文件" class="headerlink" title="打开和关闭文件"></a>打开和关闭文件</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">FILE *fp = fopen(info-&gt;software_fname,<span class="string">&quot;w&quot;</span>);</span><br><span class="line"><span class="built_in">fputs</span>(<span class="string">&quot;Enter the name of the software package manually.\n&quot;</span>,fp);</span><br><span class="line">fclose(fp);</span><br></pre></td></tr></table></figure>

<p>​		参考网站： <a target="_blank" rel="noopener" href="https://fishc.com.cn/thread-90808-1-1.html">https://fishc.com.cn/thread-90808-1-1.html</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 函数原型</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line">...</span><br><span class="line">FILE *<span class="title function_">fopen</span><span class="params">(<span class="type">const</span> <span class="type">char</span> *path, <span class="type">const</span> <span class="type">char</span> *mode)</span>;</span><br></pre></td></tr></table></figure>

<p><strong>参数解析：</strong></p>
<table>
<thead>
<tr>
<th><strong>参数</strong></th>
<th><strong>含义</strong></th>
</tr>
</thead>
<tbody><tr>
<td>path</td>
<td>该参数是一个 C 语言字符串，指定了待打开的文件路径和文件名</td>
</tr>
<tr>
<td>mode</td>
<td>“r” …如下</td>
</tr>
</tbody></table>
<p>“r”  1. 以只读的模式打开一个文本文件，从文件头开始读取  2. 该文本文件必须存在 </p>
<p>“w”1. 以只写的模式打开一个文本文件，从文件头开始写入  2. 如果文件不存在则创建一个新的文件  3. 如果文件已存在则将文件的长度截断为 0（重新写入的内容将覆盖原有的所有内容） </p>
<p>“a”1. 以追加的模式打开一个文本文件，从文件末尾追加内容  2. 如果文件不存在则创建一个新的文件</p>
<p>“r+”1. 以读和写的模式打开一个文本文件，从文件头开始读取和写入  2. 该文件必须存在  3. 该模式不会将文件的长度截断为 0（只覆盖重新写入的内容，原有的内容保留） </p>
<p>“w+”1. 以读和写的模式打开一个文本文件，从文件头开始读取和写入  2. 如果文件不存在则创建一个新的文件  3. 如果文件已存在则将文件的长度截断为 0（重新写入的内容将覆盖原有的所有内容） </p>
<p>“a+”1. 以读和追加的模式打开一个文本文件  2. 如果文件不存在则创建一个新的文件  3. 读取是从文件头开始，而写入则是在文件末尾追加</p>
<p>“b”1. 与上面 6 中模式均可结合（”rb”, “wb”, “ab”, “r+b”, “w+b”, “a+b”）  2. 其描述的含义一样，只不过操作的对象是二进制文件</p>
<p><strong>返回值：</strong></p>
<ol>
<li><p>如果文件打开成功，则返回一个指向 FILE 结构的文件指针；</p>
</li>
<li><p>如果文件打开失败，则返回 NULL 并设置 errno 为指定的错误。</p>
</li>
</ol>
<h2 id="顺序读写文件"><a href="#顺序读写文件" class="headerlink" title="顺序读写文件"></a>顺序读写文件</h2><h4 id="读写单个字符"><a href="#读写单个字符" class="headerlink" title="读写单个字符"></a>读写单个字符</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fgetc</span><span class="params">(FILE *_File)</span>;</span><br><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fputc</span><span class="params">(<span class="type">int</span> _Ch,FILE *_File)</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="读写字符串"><a href="#读写字符串" class="headerlink" title="读写字符串"></a>读写字符串</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> *__cdecl <span class="title function_">fgets</span><span class="params">(<span class="type">char</span> * __restrict__ _Buf,<span class="type">int</span> _MaxCount,FILE * __restrict__ _File)</span>;</span><br><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fputs</span><span class="params">(<span class="type">const</span> <span class="type">char</span> * __restrict__ _Str,FILE * __restrict__ _File)</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>​		注明1：_MaxCount是字符数量+1，因为末尾自动添加’\0’再返回。</p>
<p>​		注明2：如果没有到达_MaxCount就遇到了’\n’，则只读取到’\n’。</p>
<h4 id="格式化读写文件"><a href="#格式化读写文件" class="headerlink" title="格式化读写文件"></a>格式化读写文件</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fprintf</span><span class="params">(FILE * __restrict__ _File,<span class="type">const</span> <span class="type">char</span> * __restrict__ _Format,...)</span>;</span><br><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fscanf</span><span class="params">(FILE * __restrict__ _File,<span class="type">const</span> <span class="type">char</span> * __restrict__ _Format,...)</span> __MINGW_ATTRIB_DEPRECATED_SEC_WARN;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="指定尺寸读写文件"><a href="#指定尺寸读写文件" class="headerlink" title="指定尺寸读写文件"></a>指定尺寸读写文件</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">size_t</span> __cdecl <span class="title function_">fread</span><span class="params">(<span class="type">void</span> * __restrict__ _DstBuf,<span class="type">size_t</span> _ElementSize,<span class="type">size_t</span> _Count,FILE * __restrict__ _File)</span>;</span><br><span class="line"><span class="type">size_t</span> __cdecl <span class="title function_">fwrite</span><span class="params">(<span class="type">const</span> <span class="type">void</span> * __restrict__ _Str,<span class="type">size_t</span> _Size,<span class="type">size_t</span> _Count,FILE * __restrict__ _File)</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="随机读写文件"><a href="#随机读写文件" class="headerlink" title="随机读写文件"></a>随机读写文件</h2><ul>
<li><p>获取位置指示器的位置 </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">long</span> __cdecl <span class="title function_">ftell</span><span class="params">(FILE *_File)</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>设置位置指示器的位置</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fseek</span><span class="params">(FILE *_File,<span class="type">long</span> _Offset,<span class="type">int</span> _Origin)</span>;</span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> SEEK_CUR 1</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SEEK_END 2</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SEEK_SET 0</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="错误号打印"><a href="#错误号打印" class="headerlink" title="错误号打印"></a>错误号打印</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;errno.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span>&#123;</span><br><span class="line">    FILE *fp;</span><br><span class="line">    <span class="keyword">if</span> ((fp = fopen(<span class="string">&quot;aaa.txt&quot;</span>,<span class="string">&quot;r&quot;</span>)) == <span class="literal">NULL</span>)&#123;</span><br><span class="line">        perror(<span class="string">&quot;打开文件失败&quot;</span>);</span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>,<span class="string">&quot;错误：%s \n打开文件失败。\n&quot;</span>,strerror(errno));</span><br><span class="line">        <span class="built_in">exit</span>(EXIT_FAILURE);</span><br><span class="line">    &#125;</span><br><span class="line">    fclose(fp);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>​		注明： perror 和 strerror(errno) ，二选一。</p>
<h2 id="IO缓冲区"><a href="#IO缓冲区" class="headerlink" title="IO缓冲区"></a>IO缓冲区</h2><ul>
<li>手动刷新缓冲区：  fflush</li>
<li>设置缓冲区参数：  setvbuf<ul>
<li>按块缓存  _IOFBF</li>
<li>按行缓存  _IOLBF</li>
<li>不缓存      _IONBF</li>
</ul>
</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> __cdecl <span class="title function_">fflush</span><span class="params">(FILE *_File)</span>;</span><br><span class="line"><span class="type">int</span> __cdecl <span class="title function_">setvbuf</span><span class="params">(FILE * __restrict__ _File,<span class="type">char</span> * __restrict__ _Buf,<span class="type">int</span> _Mode,<span class="type">size_t</span> _Size)</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h1 id="扩展"><a href="#扩展" class="headerlink" title="扩展"></a>扩展</h1><h2 id="C语言获取Linux系统的CPU的主频"><a href="#C语言获取Linux系统的CPU的主频" class="headerlink" title="C语言获取Linux系统的CPU的主频"></a>C语言获取Linux系统的CPU的主频</h2><h4 id="直接读系统文件"><a href="#直接读系统文件" class="headerlink" title="直接读系统文件"></a>直接读系统文件</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">        FILE *fp;</span><br><span class="line">        <span class="type">char</span> str[<span class="number">81</span>];</span><br><span class="line">        <span class="built_in">memset</span>(str,<span class="number">0</span>,<span class="number">81</span>);</span><br><span class="line">        fp=popen(<span class="string">&quot;cat /proc/cpuinfo|grep cpu\\ MHz|sed -e &#x27;s/.*:[^0-9]//&#x27;&quot;</span>,<span class="string">&quot;r&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span>(fp&lt;<span class="number">0</span>)&#123;</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">&quot;无法读取 CPU主频信息\n&quot;</span>);</span><br><span class="line">                <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        fgets(str,<span class="number">80</span>,fp);</span><br><span class="line">        fclose(fp);</span><br><span class="line">        <span class="type">double</span> spd=atof(str);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;您 CPU的 主频是%fMHz\n&quot;</span>,spd);</span><br><span class="line">        <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="用C语言测试计算频率"><a href="#用C语言测试计算频率" class="headerlink" title="用C语言测试计算频率"></a>用C语言测试计算频率</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;inttypes.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/time.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">int64_t</span> <span class="title function_">rdtsc</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> i, j;</span><br><span class="line">    <span class="keyword">asm</span> <span class="title function_">volatile</span> <span class="params">(<span class="string">&quot; rdtsc&quot;</span> : <span class="string">&quot;=a&quot;</span>(i), <span class="string">&quot;=d&quot;</span>(j) : )</span>;</span><br><span class="line">    <span class="keyword">return</span> ((<span class="type">int64_t</span>)j&lt;&lt;<span class="number">32</span>) + (<span class="type">int64_t</span>)i;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int64_t</span> tsc_start, tsc_end;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">timeval</span> <span class="title">tv_start</span>, <span class="title">tv_end</span>;</span></span><br><span class="line">    <span class="type">int</span> usec_delay;</span><br><span class="line"></span><br><span class="line">    tsc_start = rdtsc();</span><br><span class="line">    gettimeofday(&amp;tv_start, <span class="literal">NULL</span>);</span><br><span class="line">    usleep(<span class="number">100000</span>);</span><br><span class="line">    tsc_end = rdtsc();</span><br><span class="line">    gettimeofday(&amp;tv_end, <span class="literal">NULL</span>);</span><br><span class="line"></span><br><span class="line">    usec_delay = <span class="number">1000000</span> * (tv_end.tv_sec - tv_start.tv_sec)</span><br><span class="line">        + (tv_end.tv_usec - tv_start.tv_usec);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot; cpu MHz\t\t: %.3f\n&quot;</span>,</span><br><span class="line">           (<span class="type">double</span>)(tsc_end-tsc_start) / usec_delay);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">[root@wei07 c]#</span><br><span class="line">[root@wei07 c]# gcc c.c -o c &amp;&amp; ./c</span><br><span class="line"> cpu MHz                : 2112.022</span><br><span class="line">[root@wei07 c]#</span><br><span class="line">[root@wei07 c]#</span><br><span class="line">[root@wei07 c]# cat /proc/cpuinfo|grep cpu\ MHz</span><br><span class="line">cpu MHz         : 2112.000</span><br><span class="line">cpu MHz         : 2112.000</span><br><span class="line">cpu MHz         : 2112.000</span><br><span class="line">cpu MHz         : 2112.000</span><br><span class="line">[root@wei07 c]#</span><br><span class="line"></span><br></pre></td></tr></table></figure>



 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%9F%BA%E7%A1%80/" rel="tag">基础</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-C语言：函数指针函数作为参数函数作为返回值"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/05/10/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E5%87%BD%E6%95%B0%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0%E5%87%BD%E6%95%B0%E4%BD%9C%E4%B8%BA%E8%BF%94%E5%9B%9E%E5%80%BC/"
    >C语言：函数指针函数作为参数函数作为返回值</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/05/10/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E5%87%BD%E6%95%B0%E4%BD%9C%E4%B8%BA%E5%8F%82%E6%95%B0%E5%87%BD%E6%95%B0%E4%BD%9C%E4%B8%BA%E8%BF%94%E5%9B%9E%E5%80%BC/" class="article-date">
  <time datetime="2023-05-10T14:14:04.000Z" itemprop="datePublished">2023-05-10</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/C%E8%AF%AD%E8%A8%80/">C语言</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="示例代码"><a href="#示例代码" class="headerlink" title="示例代码"></a>示例代码</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    函数指针            int (*fp)(int, int)</span></span><br><span class="line"><span class="comment">    函数指针作入参       </span></span><br><span class="line"><span class="comment">    函数指针作返回值  </span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">add</span><span class="params">(<span class="type">int</span>, <span class="type">int</span>)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">sub</span><span class="params">(<span class="type">int</span>, <span class="type">int</span>)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">compt</span><span class="params">(<span class="type">int</span> (*)(<span class="type">int</span>,<span class="type">int</span>),<span class="type">int</span>,<span class="type">int</span>)</span>;</span><br><span class="line"><span class="type">int</span> (*select(<span class="type">char</span>))(<span class="type">int</span>, <span class="type">int</span>);</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">add</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a+b;</span><br><span class="line">&#125;</span><br><span class="line"><span class="type">int</span> <span class="title function_">sub</span><span class="params">(<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a-b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数指针作入参的例子</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">compt</span><span class="params">(<span class="type">int</span> (*fp)(<span class="type">int</span>,<span class="type">int</span>),<span class="type">int</span> a,<span class="type">int</span> b)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (*fp)(a,b);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数指针作返回值的例子</span></span><br><span class="line"><span class="type">int</span> (*select(<span class="type">char</span> o))(<span class="type">int</span> a,<span class="type">int</span> b)&#123;</span><br><span class="line">    <span class="keyword">switch</span> (o)</span><br><span class="line">    &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;+&#x27;</span>:</span><br><span class="line">        <span class="keyword">return</span> add;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;-&#x27;</span>:</span><br><span class="line">        <span class="keyword">return</span> sub;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">int</span> rest;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 函数指针作入参的例子</span></span><br><span class="line">    rest = compt(add,<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a+b = %d\n&quot;</span>,rest);</span><br><span class="line">    rest = compt(sub,<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a-b = %d\n&quot;</span>,rest);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 函数指针作返回值的例子</span></span><br><span class="line">    <span class="type">char</span> o;</span><br><span class="line">    <span class="type">int</span> a,b;</span><br><span class="line">    <span class="type">int</span> (*fp)(<span class="type">int</span>, <span class="type">int</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;请输入计算式（例如2+3）：&quot;</span>);</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%c%d&quot;</span>,&amp;a,&amp;o,&amp;b);</span><br><span class="line">    fp = select(o);</span><br><span class="line">    rest = compt(fp,a,b);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;rest = %d\n&quot;</span>,rest);   </span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="运行结果"><a href="#运行结果" class="headerlink" title="运行结果"></a>运行结果</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[root@wei07 c]# gcc a.c &amp;&amp; a.out</span><br><span class="line">a+b = 3</span><br><span class="line">a-b = -1</span><br><span class="line">请输入计算式（例如2+3）：0-9</span><br><span class="line">rest = -9</span><br><span class="line">[root@wei07 c]#</span><br><span class="line"></span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%87%BD%E6%95%B0/" rel="tag">函数</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88/" rel="tag">函数指针</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%8C%87%E9%92%88/" rel="tag">指针</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-C语言：可变参数的函数定义"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/05/10/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0%E7%9A%84%E5%87%BD%E6%95%B0%E5%AE%9A%E4%B9%89/"
    >C语言：可变参数的函数定义</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/05/10/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0%E7%9A%84%E5%87%BD%E6%95%B0%E5%AE%9A%E4%B9%89/" class="article-date">
  <time datetime="2023-05-10T14:12:12.000Z" itemprop="datePublished">2023-05-10</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/C%E8%AF%AD%E8%A8%80/">C语言</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="示例代码"><a href="#示例代码" class="headerlink" title="示例代码"></a>示例代码</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdarg.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 可变参数的函数定义 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">sum</span><span class="params">(<span class="type">int</span> n, ...)</span>;</span><br><span class="line"><span class="type">void</span> <span class="title function_">hello</span><span class="params">(<span class="type">int</span> n, ...)</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 求和</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">sum</span><span class="params">(<span class="type">int</span> n, ...)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i, sum = <span class="number">0</span>;</span><br><span class="line">    va_list vap;</span><br><span class="line">    va_start(vap, n);</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        sum += va_arg(vap, <span class="type">int</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    va_end(vap);</span><br><span class="line">    <span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 群发</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">hello</span><span class="params">(<span class="type">int</span> n, ...)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">    va_list vap;</span><br><span class="line">    va_start(vap, n);</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;hello %s\n&quot;</span>, va_arg(vap, <span class="type">char</span> *));</span><br><span class="line">    &#125;</span><br><span class="line">    va_end(vap);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> result;</span><br><span class="line">    result = sum(<span class="number">3</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;result = %d \n&quot;</span>, result);</span><br><span class="line">    result = sum(<span class="number">5</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;result = %d \n&quot;</span>, result);</span><br><span class="line"></span><br><span class="line">    hello(<span class="number">3</span>, <span class="string">&quot;11111&quot;</span>, <span class="string">&quot;22222&quot;</span>, <span class="string">&quot;33333333&quot;</span>);</span><br><span class="line">    hello(<span class="number">2</span>, <span class="string">&quot;aaaaa&quot;</span>, <span class="string">&quot;bbbbb&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="运行结果"><a href="#运行结果" class="headerlink" title="运行结果"></a>运行结果</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">result = 6 </span><br><span class="line">result = 9 </span><br><span class="line">hello 11111</span><br><span class="line">hello 22222</span><br><span class="line">hello 33333333</span><br><span class="line">hello aaaaa</span><br><span class="line">hello bbbbb</span><br><span class="line"></span><br></pre></td></tr></table></figure>


 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%87%BD%E6%95%B0/" rel="tag">函数</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0/" rel="tag">可变参数</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-C语言：指针那点事"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/05/10/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E6%8C%87%E9%92%88%E9%82%A3%E7%82%B9%E4%BA%8B/"
    >C语言：指针那点事</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/05/10/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E6%8C%87%E9%92%88%E9%82%A3%E7%82%B9%E4%BA%8B/" class="article-date">
  <time datetime="2023-05-10T14:09:31.000Z" itemprop="datePublished">2023-05-10</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/C%E8%AF%AD%E8%A8%80/">C语言</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="C语言：指针那点事"><a href="#C语言：指针那点事" class="headerlink" title="C语言：指针那点事"></a>C语言：指针那点事</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="type">char</span> a = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">    <span class="type">char</span> *b = <span class="string">&quot;123456&quot;</span>;                <span class="comment">//    指针，字符串，字符数组；指针b指向第一个字符。                          //指向1个字符</span></span><br><span class="line">    <span class="type">char</span> *c[<span class="number">3</span>] = &#123;<span class="string">&quot;aa&quot;</span>,<span class="string">&quot;bbb&quot;</span>,<span class="string">&quot;cccc&quot;</span>&#125;;  <span class="comment">//指针数组，字符串数组，字符串首地址数组；数组c包含3个char类型的指针地址。     //数组包含3个char类型指针</span></span><br><span class="line">    <span class="type">int</span> d[<span class="number">2</span>][<span class="number">3</span>] = &#123;                    <span class="comment">//二维数组，整形数组；数组d。</span></span><br><span class="line">        &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;,</span><br><span class="line">        &#123;<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>&#125;,</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="type">char</span> * ap = &amp;a;       </span><br><span class="line">    <span class="type">char</span> * bp = b;        <span class="comment">//指向1个字符</span></span><br><span class="line">    <span class="type">char</span> * * cp = c;      <span class="comment">//指向1个指针</span></span><br><span class="line">    <span class="type">int</span> (* dp) [<span class="number">3</span>] = d;   <span class="comment">//指向1个int[3]</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c\n&quot;</span>, *ap);   <span class="comment">//解引用</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%c%c%c%c%c%c\n&quot;</span>, *bp,*(bp+<span class="number">1</span>),*(bp+<span class="number">2</span>),*(bp+<span class="number">3</span>),*(bp+<span class="number">4</span>),*(bp+<span class="number">5</span>)); <span class="comment">//漂移+解引用</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%s%s%s\n&quot;</span>,*cp,*(cp+<span class="number">1</span>),*(cp+<span class="number">2</span>),*(cp+<span class="number">3</span>));                        <span class="comment">//漂移+解引用</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%s%s%s\n&quot;</span>,*c,*(c+<span class="number">1</span>),*(c+<span class="number">2</span>),*(c+<span class="number">3</span>));                        <span class="comment">//数组和指针之间有着深意</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%2d%2d%2d\n&quot;</span>,(*dp+<span class="number">0</span>)[<span class="number">0</span>],(*dp+<span class="number">0</span>)[<span class="number">1</span>],(*dp+<span class="number">0</span>)[<span class="number">2</span>]);                <span class="comment">//漂移+解引用+下标</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%2d%2d%2d\n&quot;</span>,**(dp+<span class="number">1</span>),*(*(dp+<span class="number">1</span>)+<span class="number">1</span>),*(*(dp+<span class="number">1</span>)+<span class="number">2</span>));              <span class="comment">//漂移+解引用+解引用</span></span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="运行结果"><a href="#运行结果" class="headerlink" title="运行结果"></a>运行结果</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">a</span><br><span class="line">123456</span><br><span class="line">aabbbcccc</span><br><span class="line">aabbbcccc</span><br><span class="line"> 1 2 3</span><br><span class="line"> 4 5 6</span><br></pre></td></tr></table></figure>

<h3 id="核心心法"><a href="#核心心法" class="headerlink" title="核心心法"></a>核心心法</h3><pre><code>指针指万物。
</code></pre>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%8C%87%E9%92%88/" rel="tag">指针</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%95%B0%E7%BB%84/" rel="tag">数组</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-GBase8s下载备份所有TBLSpace页"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/05/10/GBase8s%E4%B8%8B%E8%BD%BD%E5%A4%87%E4%BB%BD%E6%89%80%E6%9C%89TBLSpace%E9%A1%B5/"
    >GBase8s下载备份所有TBLSpace页</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/05/10/GBase8s%E4%B8%8B%E8%BD%BD%E5%A4%87%E4%BB%BD%E6%89%80%E6%9C%89TBLSpace%E9%A1%B5/" class="article-date">
  <time datetime="2023-05-10T02:00:00.000Z" itemprop="datePublished">2023-05-10</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/GBase8s/">GBase8s</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="dumpTBLSall-c-shell版"><a href="#dumpTBLSall-c-shell版" class="headerlink" title="dumpTBLSall c+shell版"></a>dumpTBLSall c+shell版</h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">char</span>* <span class="title function_">itoa</span><span class="params">(<span class="type">int</span> num,<span class="type">char</span>* str,<span class="type">int</span> radix)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">char</span> index[]=<span class="string">&quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;</span>;</span><br><span class="line">    <span class="type">unsigned</span> unum;</span><br><span class="line">    <span class="type">int</span> i=<span class="number">0</span>,j,k;</span><br><span class="line"> </span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span>(radix==<span class="number">10</span>&amp;&amp;num&lt;<span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        unum=(<span class="type">unsigned</span>)-num;</span><br><span class="line">        str[i++]=<span class="string">&#x27;-&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> unum=(<span class="type">unsigned</span>)num;</span><br><span class="line"> </span><br><span class="line">    </span><br><span class="line">    <span class="keyword">do</span></span><br><span class="line">    &#123;</span><br><span class="line">        str[i++]=index[unum%(<span class="type">unsigned</span>)radix];</span><br><span class="line">        unum/=radix;</span><br><span class="line"> </span><br><span class="line">    &#125;<span class="keyword">while</span>(unum);</span><br><span class="line"> </span><br><span class="line">    str[i]=<span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line"> </span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span>(str[<span class="number">0</span>]==<span class="string">&#x27;-&#x27;</span>) k=<span class="number">1</span>;</span><br><span class="line">    <span class="keyword">else</span> k=<span class="number">0</span>;</span><br><span class="line"> </span><br><span class="line">    <span class="type">char</span> temp;</span><br><span class="line">    <span class="keyword">for</span>(j=k;j&lt;=(i<span class="number">-1</span>)/<span class="number">2</span>;j++)</span><br><span class="line">    &#123;</span><br><span class="line">        temp=str[j];</span><br><span class="line">        str[j]=str[i<span class="number">-1</span>+k-j];</span><br><span class="line">        str[i<span class="number">-1</span>+k-j]=temp;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">return</span> str;</span><br><span class="line"> </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">writePE</span><span class="params">()</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> system(<span class="string">&quot;oncheck -pe &gt; pe&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">wstrtok</span><span class="params">(<span class="type">char</span> *str, <span class="type">char</span> s[<span class="number">2</span>], <span class="type">int</span> n, <span class="type">char</span> *<span class="built_in">list</span>[])</span>&#123;</span><br><span class="line">        <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">list</span>[<span class="number">0</span>]=strtok(str,s);</span><br><span class="line">        <span class="keyword">while</span>(<span class="built_in">list</span>[i] != <span class="literal">NULL</span>)&#123;</span><br><span class="line">                <span class="built_in">list</span>[++i] = strtok(<span class="literal">NULL</span>,s);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;n; i++)&#123;</span><br><span class="line">                <span class="keyword">if</span>(<span class="built_in">list</span>[i] != <span class="literal">NULL</span>)&#123;</span><br><span class="line">                        <span class="comment">//printf(&quot;%d: %s\n&quot;,i,list[i]);</span></span><br><span class="line">                &#125;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">dumpAllTBLS</span><span class="params">()</span>&#123;</span><br><span class="line">        FILE *pe;</span><br><span class="line">        pe = fopen(<span class="string">&quot;pe&quot;</span>,<span class="string">&quot;r&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span> (pe == <span class="literal">NULL</span>)&#123;</span><br><span class="line">                <span class="built_in">fputs</span>(<span class="string">&quot;打开文件出错!\n&quot;</span>,<span class="built_in">stderr</span>);</span><br><span class="line">                <span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">char</span> h[<span class="number">1024</span>];</span><br><span class="line">        <span class="type">char</span> path[<span class="number">200</span>];</span><br><span class="line">        <span class="type">int</span> pgsize;</span><br><span class="line">        <span class="type">int</span> begin ;</span><br><span class="line">        <span class="type">int</span> lens ;</span><br><span class="line">        <span class="keyword">while</span> (!feof(pe))&#123;</span><br><span class="line">                fgets(h,<span class="number">1025</span>,pe);</span><br><span class="line">                <span class="keyword">if</span>( ferror(pe) )&#123;</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span>(<span class="built_in">strstr</span>(h,<span class="string">&quot;TBLSpace&quot;</span>) || <span class="built_in">strstr</span>(h,<span class="string">&quot; /&quot;</span>))&#123;</span><br><span class="line">                        <span class="keyword">if</span>(!<span class="built_in">strstr</span>(h,<span class="string">&quot;(0x&quot;</span>))&#123;</span><br><span class="line">                                <span class="keyword">if</span>(<span class="built_in">strstr</span>(h,<span class="string">&quot; /&quot;</span>))&#123;</span><br><span class="line">                                        <span class="type">char</span> *<span class="built_in">list</span>[<span class="number">6</span>];</span><br><span class="line">                                        wstrtok(h,<span class="string">&quot; &quot;</span>,<span class="number">6</span>,<span class="built_in">list</span>);</span><br><span class="line">                                        <span class="built_in">strcpy</span>(path,<span class="built_in">list</span>[<span class="number">1</span>]);</span><br><span class="line">                                        pgsize = atoi(<span class="built_in">list</span>[<span class="number">2</span>]);</span><br><span class="line">                                &#125;</span><br><span class="line">                                <span class="keyword">if</span>(<span class="built_in">strstr</span>(h,<span class="string">&quot;TBLSpace&quot;</span>))&#123;</span><br><span class="line">                                        <span class="comment">//printf(&quot;%s&quot;,h);</span></span><br><span class="line">                                        <span class="type">char</span> *ltbl[<span class="number">3</span>];</span><br><span class="line">                                        wstrtok(h,<span class="string">&quot; &quot;</span>,<span class="number">3</span>,ltbl);</span><br><span class="line">                                        begin = atoi(ltbl[<span class="number">1</span>]);</span><br><span class="line">                                        lens = atoi(ltbl[<span class="number">2</span>]);</span><br><span class="line">                                        <span class="built_in">printf</span>(<span class="string">&quot;#%s %d %d %d\n&quot;</span>,path,pgsize,begin,lens);</span><br><span class="line">                                        <span class="type">int</span> i = begin;</span><br><span class="line">                                        <span class="type">int</span> end = begin + lens;</span><br><span class="line">                                        <span class="type">char</span> <span class="built_in">string</span>[<span class="number">1024</span>] ;</span><br><span class="line">                                        <span class="built_in">strcpy</span>(<span class="built_in">string</span>,path);</span><br><span class="line">                                        <span class="type">const</span> <span class="type">char</span> sd[<span class="number">2</span>] = <span class="string">&quot;/&quot;</span>;</span><br><span class="line">                                        <span class="type">char</span> *name;</span><br><span class="line">                                        <span class="type">char</span> *lastname;</span><br><span class="line">                                        name = strtok(<span class="built_in">string</span>,sd);</span><br><span class="line">                                        <span class="keyword">while</span>( name != <span class="literal">NULL</span> )&#123;</span><br><span class="line">                                                lastname=name;</span><br><span class="line">                                                name = strtok(<span class="literal">NULL</span>,sd);</span><br><span class="line">                                        &#125;</span><br><span class="line">                                        <span class="built_in">strcat</span>(lastname,<span class="string">&quot;_&quot;</span>);</span><br><span class="line">                                        <span class="comment">//printf(&quot;%s====%s=====%s====\n&quot;,name,lastname,path);</span></span><br><span class="line">                                        <span class="type">char</span> strc[<span class="number">200</span>] = &#123;<span class="number">0</span>&#125; ;</span><br><span class="line">                                        <span class="type">char</span> strt[<span class="number">200</span>] = &#123;<span class="number">0</span>&#125; ;</span><br><span class="line">                                        <span class="type">char</span> pgsizes[<span class="number">20</span>] ;</span><br><span class="line">                                        <span class="type">char</span> is[<span class="number">20</span>];</span><br><span class="line">                                        system(<span class="string">&quot;mkdir -p pag; mkdir -p txt; &quot;</span>);</span><br><span class="line">                                        <span class="comment">//itoa(pgsize,pgsizes,16);</span></span><br><span class="line">                                        <span class="built_in">sprintf</span>(pgsizes,<span class="string">&quot;%d&quot;</span>,pgsize*<span class="number">1024</span>);</span><br><span class="line">                                        <span class="keyword">while</span> ( i &lt; end )&#123;</span><br><span class="line">                                                <span class="comment">//itoa(i,is,16);</span></span><br><span class="line">                                                <span class="built_in">sprintf</span>(is,<span class="string">&quot;%d&quot;</span>,i);</span><br><span class="line">                                                <span class="built_in">strcpy</span>(strc,<span class="string">&quot;dd if=&quot;</span>);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strc,path);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strc,<span class="string">&quot; of=pag/&quot;</span>);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strc,lastname);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strc,is);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strc,<span class="string">&quot; skip=&quot;</span>);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strc,is);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strc,<span class="string">&quot; bs=&quot;</span>);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strc,pgsizes);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strc,<span class="string">&quot; count=1&quot;</span>);</span><br><span class="line">                                                <span class="comment">//printf(&quot;%s\n&quot;,strc);</span></span><br><span class="line">                                                system(strc);</span><br><span class="line"></span><br><span class="line">                                                <span class="built_in">strcpy</span>(strt,<span class="string">&quot;xxd pag/&quot;</span>);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strt,lastname);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strt,is);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strt,<span class="string">&quot; &gt; txt/&quot;</span>);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strt,lastname);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strt,is);</span><br><span class="line">                                                <span class="built_in">strcat</span>(strt,<span class="string">&quot;.txt&quot;</span>);</span><br><span class="line">                                                <span class="comment">//printf(&quot;%s\n&quot;,strt);</span></span><br><span class="line">                                                system(strt);</span><br><span class="line">                                                i++;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">        <span class="type">int</span> j;</span><br><span class="line">    	j = writePE();</span><br><span class="line">        <span class="keyword">if</span> ( j != <span class="number">0</span> ) &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;获取pe失败\n&quot;</span>);</span><br><span class="line">			<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        dumpAllTBLS();</span><br><span class="line">        system(<span class="string">&quot;tar -zcvf pag.tar.gz pag/&quot;</span>);</span><br><span class="line">        system(<span class="string">&quot;tar -zcvf txt.tar.gz txt/&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/GBase8s/" rel="tag">GBase8s</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/level3/" rel="tag">level3</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-GBase8s集群管理程序WeiCM添加RSS的自动备份恢复"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/05/10/GBase8s%E9%9B%86%E7%BE%A4%E7%AE%A1%E7%90%86%E7%A8%8B%E5%BA%8FWeiCM%E6%B7%BB%E5%8A%A0RSS%E7%9A%84%E8%87%AA%E5%8A%A8%E5%A4%87%E4%BB%BD%E6%81%A2%E5%A4%8D/"
    >GBase8s集群管理程序WeiCM添加RSS的自动备份恢复</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/05/10/GBase8s%E9%9B%86%E7%BE%A4%E7%AE%A1%E7%90%86%E7%A8%8B%E5%BA%8FWeiCM%E6%B7%BB%E5%8A%A0RSS%E7%9A%84%E8%87%AA%E5%8A%A8%E5%A4%87%E4%BB%BD%E6%81%A2%E5%A4%8D/" class="article-date">
  <time datetime="2023-05-10T01:52:27.000Z" itemprop="datePublished">2023-05-10</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Develop/">Develop</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="何时做备份恢复"><a href="#何时做备份恢复" class="headerlink" title="何时做备份恢复"></a>何时做备份恢复</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">情况1，备机看到这个：</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">tail</span> -20  /opt/gbase/tmp/online.log |grep <span class="string">&quot;Log Record Apply Thread Exited Abnormally&quot;</span></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">情况1，主机看到这个：</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">16:31:44  Error receiving a buffer from RSS gbase02 - shutting down</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">16:31:44  RSS Server gbase02 - state is now disconnected</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">情况2，RSS需要的日志已经滚圈了</span></span><br></pre></td></tr></table></figure>

<h2 id="做备份恢复的脚本"><a href="#做备份恢复的脚本" class="headerlink" title="做备份恢复的脚本"></a>做备份恢复的脚本</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cat &gt;  /home/gbase/sysInfo/WeiShell/WeiRssResort.sh</span><br></pre></td></tr></table></figure>



<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="comment">#####################################################################################</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="comment">#情况1，备机看到这个：</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">tail</span> -20  /opt/gbase/tmp/online.log |grep <span class="string">&quot;Log Record Apply Thread Exited Abnormally&quot;</span></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="comment">#情况2，主机看到这个：</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">16:31:44  Error receiving a buffer from RSS gbase02 - shutting down</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">16:31:44  RSS Server gbase02 - state is now disconnected</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="comment">#情况3，RSS需要的日志已经滚圈了</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="comment">#####################################################################################</span></span></span><br><span class="line"></span><br><span class="line">. /home/gbase/.bash_profile</span><br><span class="line">cd /home/gbase/sysInfo/WeiShell</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">BFHF()&#123;</span><br><span class="line">	</span><br><span class="line">ssh informix@$ip &quot;. /home/gbase/.bash_profile &amp;&amp; onmode -ky; onclean -ky &quot;</span><br><span class="line"></span><br><span class="line">ontape -s -L 0 -t STDIO | ssh informix@$ip &quot;. /home/gbase/.bash_profile &amp;&amp; ontape -p -t STDIO&quot; </span><br><span class="line"></span><br><span class="line">onmode -d add RSS $rssname</span><br><span class="line"></span><br><span class="line">echo &quot;ssh informix@$ip \&quot;. /home/gbase/.bash_profile &amp;&amp; onmode -d RSS $INFORMIXSERVER \&quot;&quot; |sh</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">while true </span><br><span class="line">do</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">自己不是主，自己dbaccess等验证不成功，继续下次循环</span></span><br><span class="line">zhutrue=$(./GetNodeInfo.sh |awk -F \| &#x27;&#123;print $2$3$5$8&#125;&#x27;)</span><br><span class="line">if [ $zhutrue != &quot;primarytruetruetrue&quot; ]; then</span><br><span class="line">	echo &quot;`date` wo bu shi zhu jie dian.&quot;</span><br><span class="line">    sleep 60</span><br><span class="line">	continue</span><br><span class="line">fi</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">在自己的日志发现有rss节点日志回滚冲突，重新恢复这个节点</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">ApplyErr=$(onstat -m |grep <span class="string">&quot;Error receiving a buffer from &quot;</span>|<span class="built_in">wc</span> -l)</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">rssname=$(onstat -m |grep <span class="string">&quot;Error receiving a buffer from &quot;</span>|<span class="built_in">tail</span> -1|awk  <span class="string">&#x27;&#123;print $8&#125;&#x27;</span>)</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="keyword">if</span> [ <span class="variable">$ApplyErr</span> -gt 0 ] ; <span class="keyword">then</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">		onstat -m |grep <span class="string">&quot;RSS Server <span class="variable">$rssname</span> - state is now disconnected&quot;</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">		<span class="keyword">if</span> [ $? -eq 0 ]; <span class="keyword">then</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">			<span class="built_in">echo</span> <span class="string">&quot;`date` kai shi hui fu <span class="variable">$rssname</span>&quot;</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">			BFHF</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">		<span class="keyword">fi</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="keyword">fi</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">onstat -g rss|grep &#x27;,&#x27;|sed 1d|while read line</span><br><span class="line">do</span><br><span class="line">    rssname=$(echo $line|awk  &#x27;&#123;print $1&#125;&#x27;)</span><br><span class="line">    ip=$(cat /opt/gbase/etc/sqlhosts |awk -v h=$rssname &#x27;$1==h &#123;print $3&#125;&#x27;)</span><br><span class="line"></span><br><span class="line">    #有rss日志滚圈了，重新恢复这个节点</span><br><span class="line">    rssaddr=$(echo $line|awk -F , &#x27;&#123;print $1&#125;&#x27;|awk &#x27;&#123;print $NF&#125;&#x27;)</span><br><span class="line">    youwu=$(onstat -l |grep &#x27;U&#x27;|grep -v Version|awk -v i=$rssaddr &#x27;$4==i&#x27;|wc -l)</span><br><span class="line">    if [ $youwu -eq  0 ]; then</span><br><span class="line">    	echo &quot;`date` kai shi hui fu $rssname, because log 1 quan.&quot;</span><br><span class="line">        BFHF</span><br><span class="line">    fi</span><br><span class="line"></span><br><span class="line">    #查看一些备机的运行情况，如果连续5次unknow/norun和Dbaccess:false</span><br><span class="line">    #1 : main.NodeCurr&#123;ServName:&quot;gbase02&quot;, Mode:&quot;unknow&quot;, Active:false, PingIN:false, PingOUT:false, LogUniqid:0, LogPercent:0, Dbaccess:false, CurrTime:&quot;2022-11-08 05:07:41&quot;&#125;</span><br><span class="line">    n=1</span><br><span class="line">    errrss=0</span><br><span class="line">    while [ $n -le 5 ] ; do</span><br><span class="line">        rsstrue=$(echo &quot;ssh informix@$ip \&quot;. /home/gbase/.bash_profile &amp;&amp; /home/gbase/sysInfo/WeiShell/GetNodeInfo.sh \&quot;&quot; |sh |awk -F \| &#x27;&#123;print $2$8&#125;&#x27;)</span><br><span class="line">        if [ $rsstrue == &quot;unknowfalse&quot; ]; then</span><br><span class="line">            let errrss=$errrss+1</span><br><span class="line">        fi</span><br><span class="line">        if [ $rsstrue == &quot;norunfalse&quot; ]; then</span><br><span class="line">            let errrss=$errrss+1</span><br><span class="line">        fi</span><br><span class="line">        let n=$n+1</span><br><span class="line">        sleep 10</span><br><span class="line">    done</span><br><span class="line">    if [ $errrss -eq 5 ] ; then</span><br><span class="line">        echo &quot;`date` kai shi hui fu $rssname, because rss Apply err.&quot;</span><br><span class="line">        BFHF</span><br><span class="line">    fi</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">echo &quot;`date` all rss running ok.&quot;</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">chmod 755  /home/gbase/sysInfo/WeiShell/WeiRssResort.sh</span><br></pre></td></tr></table></figure>



<h2 id="备份恢复脚本开启启动"><a href="#备份恢复脚本开启启动" class="headerlink" title="备份恢复脚本开启启动"></a>备份恢复脚本开启启动</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cat &gt;&gt; /etc/rc.local</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">su - informix -c &quot;cd /home/gbase/sysInfo/WeiShell; ./WeiRssResort.sh &gt; /tmp/WEICMLOG/WeiRssResort.log 2&gt;&amp;1 &amp; &quot;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Ctrl + D</span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E8%87%AA%E7%A0%94/" rel="tag">自研</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E9%9B%86%E7%BE%A4/" rel="tag">集群</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E9%AB%98%E5%8F%AF%E7%94%A8/" rel="tag">高可用</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
  </article>
  

  
  <nav class="page-nav">
    
    <a class="extend prev" rel="prev" href="/">上一页</a><a class="page-number" href="/">1</a><span class="page-number current">2</span><a class="page-number" href="/page/3/">3</a><a class="page-number" href="/page/4/">4</a><span class="space">&hellip;</span><a class="page-number" href="/page/6/">6</a><a class="extend next" rel="next" href="/page/3/">下一页</a>
  </nav>
  
</section>
</div>

      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2023-2024
        <i class="ri-heart-fill heart_icon"></i> Wang Wei
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ww.ico" alt="朝花夕拾"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->

<script src="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.staticfile.org/justifiedGallery/3.8.1/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.staticfile.org/clipboard.js/2.0.10/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>